AssetBundles
Posted Tekkaman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AssetBundles相关的知识,希望对你有一定的参考价值。
【AssetBundles】
AssetBundles can express dependencies between each other; e.g. a material in AssetBundle A can reference a texture in AssetBundle B. For efficient delivery over networks, AssetBundles can be compressed with a choice of built-in algorithms depending on use case requirements (LZMA and LZ4).
AssetBundles can be useful for downloadable content (DLC), reducing initial install size, loading assets optimized for the end-user’s platform, and reduce runtime memory pressure.
Note that AssetBundle names do support a type of folder structure depending on what you type. To add sub folders, separate folder names by a “/”. For example: AssetBundle name “environment/forest” will create a bundle named forest under an environment sub folder.
1、添加一个菜单选项,以来Build AssetBundle。
When you click Build AssetBundles a progress bar will appear with a build dialogue. This will take all the assets you labeled with an AssetBundle name in and place them in a folder at the path defined by assetBundleDirectory.
using UnityEditor; public class CreateAssetBundles { [MenuItem("Assets/Build AssetBundles")] static void BuildAllAssetBundles() { string assetBundleDirectory = "Assets/AssetBundles"; if(!Directory.Exists(assetBundleDirectory) { Directory.CreateDirectory(assetBundleDirectory); } BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.Standalone); } }
2、通过 LoadFromFile 加载AssetBundle。
public class LoadFromFileExample extends MonoBehaviour { function Start() { var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle")); if (myLoadedAssetBundle == null) { Debug.Log("Failed to load AssetBundle!"); return; } var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject"); Instantiate(prefab); } }
3、使用UnityWebRequest下载,并使用DownloadHandlerAssetBundle来加载。
GetAssetBundle(string, int)
takes the uri of the location of the AssetBundle and the version of the bundle you want to download.
The UnityWebRequest has a specific handle for dealing with AssetBundles, DownloadHandlerAssetBundle
, which gets the AssetBundle from the request.
IEnumerator InstantiateObject() { string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName; UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0); yield return request.Send(); AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); GameObject cube = bundle.LoadAsset<GameObject>("Cube"); GameObject sprite = bundle.LoadAsset<GameObject>("Sprite"); Instantiate(cube); Instantiate(sprite); }
【使用AssetBundle】
1、通过加密AssetBundle内的内容,来保护数据。
1 string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d"; 2 IEnumerator Start () { 3 // Start a download of the encrypted assetbundle 4 WWW www = new WWW.LoadFromCacheOrDownload (url, 1); 5 6 // Wait for download to complete 7 yield return www; 8 9 // Load the TextAsset from the AssetBundle 10 TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset)); 11 12 // Get the byte data 13 byte[] encryptedData = textAsset.bytes; 14 15 // Decrypt the AssetBundle data 16 byte[] decryptedData = YourDecryptionMethod(encryptedData); 17 18 // Use your byte array. The AssetBundle will be cached 19 }
2、通过加密AssetBundle,来保护数据。
1 string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d"; 2 IEnumerator Start () { 3 // Start a download of the encrypted assetbundle 4 WWW www = new WWW (url); 5 6 // Wait for download to complete 7 yield return www; 8 9 // Get the byte data 10 byte[] encryptedData = www.bytes; 11 12 // Decrypt the AssetBundle data 13 byte[] decryptedData = YourDecryptionMethod(encryptedData); 14 15 // Create an AssetBundle from the bytes array 16 17 AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData); 18 yield return acr; 19 20 AssetBundle bundle = acr.assetBundle; 21 22 // You can now use your AssetBundle. The AssetBundle is not cached. 23 }
3、一个AssetBundle中加入另一个加密过的AssetBundel。
1 string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d"; 2 IEnumerator Start () { 3 // Start a download of the encrypted assetbundle 4 WWW www = new WWW.LoadFromCacheOrDownload (url, 1); 5 6 // Wait for download to complete 7 yield return www; 8 9 // Load the TextAsset from the AssetBundle 10 TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset)); 11 12 // Get the byte data 13 byte[] encryptedData = textAsset.bytes; 14 15 // Decrypt the AssetBundle data 16 byte[] decryptedData = YourDecryptionMethod(encryptedData); 17 18 // Create an AssetBundle from the bytes array 19 AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData); 20 yield return acr; 21 22 AssetBundle bundle = acr.assetBundle; 23 24 // You can now use your AssetBundle. The wrapper AssetBundle is cached 25 }
4、二进制数据必须以.bytes结尾,才能保存进AssetBundle,类型是TextAsset。
1 string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d"; 2 IEnumerator Start () { 3 // Start a download of the given URL 4 WWW www = WWW.LoadFromCacheOrDownload (url, 1); 5 6 // Wait for download to complete 7 yield return www; 8 9 // Load and retrieve the AssetBundle 10 AssetBundle bundle = www.assetBundle; 11 12 // Load the TextAsset object 13 TextAsset txt = bundle.Load("myBinaryAsText", typeof(TextAsset)) as TextAsset; 14 15 // Retrieve the binary data as an array of bytes 16 byte[] bytes = txt.bytes; 17 }
5、只能加载一个同名AssetBundle。
6、AssetBundle.LoadAssetAsync。
1 using UnityEngine; 2 3 // Note: This example does not check for errors. Please look at the example in the DownloadingAssetBundles section for more information 4 IEnumerator Start () { 5 // Start a download of the given URL 6 WWW www = WWW.LoadFromCacheOrDownload (url, 1); 7 8 // Wait for download to complete 9 yield return www; 10 11 // Load and retrieve the AssetBundle 12 AssetBundle bundle = www.assetBundle; 13 14 // Load the object asynchronously 15 AssetBundleRequest request = bundle.LoadAssetAsync ("myObject", typeof(GameObject)); 16 17 // Wait for completion 18 yield return request; 19 20 // Get the reference to the loaded object 21 GameObject obj = request.asset as GameObject; 22 23 // Unload the AssetBundles compressed contents to conserve memory 24 bundle.Unload(false); 25 26 // Frees the memory from the web stream 27 www.Dispose(); 28 }
7、
以上是关于AssetBundles的主要内容,如果未能解决你的问题,请参考以下文章
Unity3D技术文档翻译第1.8篇 AssetBundles 问题及解决方法
Unity AssetBundles and Resources指引