首页 > 代码库 > AssetBundles

AssetBundles

AssetBundles

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 AssetBundle10     TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset));11  12     // Get the byte data13     byte[] encryptedData =http://www.mamicode.com/ textAsset.bytes;14 15     // Decrypt the AssetBundle data16     byte[] decryptedData =http://www.mamicode.com/ YourDecryptionMethod(encryptedData);17 18     // Use your byte array. The AssetBundle will be cached19 }
View Code

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 data10     byte[] encryptedData =http://www.mamicode.com/ www.bytes;11 12     // Decrypt the AssetBundle data13     byte[] decryptedData =http://www.mamicode.com/ YourDecryptionMethod(encryptedData);14 15     // Create an AssetBundle from the bytes array16 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 }
View Code

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 AssetBundle10     TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset));11  12     // Get the byte data13     byte[] encryptedData =http://www.mamicode.com/ textAsset.bytes;14 15     // Decrypt the AssetBundle data16     byte[] decryptedData =http://www.mamicode.com/ YourDecryptionMethod(encryptedData);17 18     // Create an AssetBundle from the bytes array19     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 cached25 }
View Code

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 AssetBundle10     AssetBundle bundle = www.assetBundle;11 12     // Load the TextAsset object13     TextAsset txt = bundle.Load("myBinaryAsText", typeof(TextAsset)) as TextAsset;14 15     // Retrieve the binary data as an array of bytes16     byte[] bytes = txt.bytes;17 }
View Code

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 AssetBundle12     AssetBundle bundle = www.assetBundle;13 14     // Load the object asynchronously15     AssetBundleRequest request = bundle.LoadAssetAsync ("myObject", typeof(GameObject));16 17     // Wait for completion18     yield return request;19 20     // Get the reference to the loaded object21     GameObject obj = request.asset as GameObject;22 23         // Unload the AssetBundles compressed contents to conserve memory24         bundle.Unload(false);25 26         // Frees the memory from the web stream27         www.Dispose();28 }
View Code

7、

 

AssetBundles