天天看点

AssetBundle系列——共享资源打包/依赖资源打包

转自http://www.cnblogs.com/sifenkesi/p/3915477.html 

有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数:

  BuildPipeline.PushAssetDependencies():依赖资源压栈;

  BuildPipeline.PopAssetDependencies():依赖资源出栈。

  直接看代码,下面为打包示例代码,Prefab1和Prefab2共享贴图资源Tex1,在打包时将Tex1单独打包,而Prefab1和Prefab2对应的assetbundle包中不实际包含Tex1资源,而是记录Tex1资源的引用:

using UnityEditor;

using UnityEngine;

using System.IO;

using System.Collections;

using System.Collections.Generic;

public class PushAndPop 

{

    [MenuItem("Test/BuildAssetBundle")]

    static void Execute()

    {

        string SavePath = "C:\\";

        BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets 

            | BuildAssetBundleOptions.DeterministicAssetBundle;

        BuildPipeline.PushAssetDependencies();

        // 共享资源Tex1.tga

        Object sharedAsset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/Tex1.tga");

        BuildPipeline.BuildAssetBundle(sharedAsset, null, SavePath + sharedAsset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);

        // Prefab1,引用了Tex1.tga

        Object p1Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P1.prefab");

        BuildPipeline.BuildAssetBundle(p1Asset, null, SavePath + p1Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);

        BuildPipeline.PopAssetDependencies();

        // Prefab2,引用了Tex1.tga

        Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P2.prefab");

        BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows);

        EditorUtility.DisplayDialog("", "Completed", "OK");

        AssetDatabase.Refresh();

    }

}

可以看到,Push和Pos都是成对使用,一个Push/Pop对就相当于一个Layer(层),层可以嵌套,内层可以依赖外层的资源。也就是说内层某资源在打包时,如果其引用的某个资源已经在外层加载了,那么内层的这个资源包就会包含该资源的引用而不是资源本身。Push/Pop实际上维持了一个依赖的堆栈。

  那么,在加载依赖资源包时,需要注意的是:先加载依赖的资源,然后加载其他资源,需要确保这个顺序。下面的代码演示如何使用依赖资源包:

public class NewBehaviourScript : MonoBehaviour 

    void OnGUI()

        // 清空本地缓存

        if (GUI.Button(new Rect(0f, 0f, 100f, 20f), Caching.spaceOccupied.ToString()))

        {

            Caching.CleanCache();

        }

        if (GUI.Button(new Rect(0f, 30f, 100f, 20f), "Load Share Res"))

            StartCoroutine(Load(@"file://C:\Tex1.assetbundle", 1));

        if (GUI.Button(new Rect(0f, 60f, 100f, 20f), "Load And Instantiate Prefab"))

            StartCoroutine(LoadAndInstantiate(@"file://C:\P1.assetbundle", 1));

            StartCoroutine(LoadAndInstantiate(@"file://C:\P2.assetbundle", 1));

    // 加载

    IEnumerator Load(string url, int version)

        WWW www = WWW.LoadFromCacheOrDownload(url, version);

        yield return www;

    // 加载并实例化

    IEnumerator LoadAndInstantiate(string url, int version)

        if (!System.String.IsNullOrEmpty(www.error))

            Debug.Log(www.error);

        else

            Object main = www.assetBundle.mainAsset;

            GameObject.Instantiate(main);

先按第二个按钮,然后再按第三个按钮就能够正确显示两个Prefab,如果直接进行第三步操作,则实例化出来的Prefab会缺少贴图。

  另外我们可以从assetbundle包的大小来看一下:

  如果不打依赖包,两个prefab都打完整包,得到的包的大小为:

  P1.assetbundle      56K

  P2.assetbundle      38K

  如果打依赖包,得到的包的大小为:

  Tex1.assetbundle      10k

  P1.assetbundle      47K