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);
    }
}
View Code

 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);
    }
}
View Code

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);
    }
View Code

使用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 }
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 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 }
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 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 }
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 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 }
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 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 }
View Code

7、

相关推荐