有人在之前的博客中問我有關共享資源打包的代碼,其實這一塊很簡單,就兩個函數:
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 BuildPipeline.PushAssetDependencies(); 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 BuildPipeline.PushAssetDependencies(); Object p2Asset = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Test/P2.prefab"); BuildPipeline.BuildAssetBundle(p2Asset, null, SavePath + p2Asset.name + ".assetbundle", buildOp, BuildTarget.StandaloneWindows); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies(); EditorUtility.DisplayDialog("", "Completed", "OK"); AssetDatabase.Refresh(); } }
可以看到,Push和Pos都是成對使用,一個Push/Pop對就相當於一個Layer(層),層可以嵌套,內層可以依賴外層的資源。也就是說內層某資源在打包時,如果其引用的某個資源已經在外層加載了,那么內層的這個資源包就會包含該資源的引用而不是資源本身。Push/Pop實際上維持了一個依賴的堆棧。
那么,在加載依賴資源包時,需要注意的是:先加載依賴的資源,然后加載其他資源,需要確保這個順序。下面的代碼演示如何使用依賴資源包:
using UnityEngine; using System.Collections; 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) { WWW www = WWW.LoadFromCacheOrDownload(url, version); yield return www; 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
P2.assetbundle 29K
規律是不是很明顯。
