日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AssetBundle系列——共享资源打包/依赖资源打包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  有人在之前的博客中問我有關(guān)共享資源打包的代碼,其實這一塊很簡單,就兩個函數(shù):

  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.tgaObject 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(層),層可以嵌套,內(nèi)層可以依賴外層的資源。也就是說內(nèi)層某資源在打包時,如果其引用的某個資源已經(jīng)在外層加載了,那么內(nèi)層的這個資源包就會包含該資源的引用而不是資源本身。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

  規(guī)律是不是很明顯。

總結(jié)

以上是生活随笔為你收集整理的AssetBundle系列——共享资源打包/依赖资源打包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。