1、導出assetbundle:
①單個資源導出成assetbundle;
②多個資源導出成一個assetbundle;
2、讀取assetbundle:
①加載到內(nèi)存;
②解壓為具體資源。
1、導出assetbundle:
①單個資源導出成assetbundle;
BuildPipeline.BuildAssetBundle(Object obj, null, string path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);//obj 單個資源,轉(zhuǎn)成Object類型//path 路徑(例如:"Assets/streamingassets/obj.unity3d"),資源包的后綴名可以任意寫或不寫//BuildAssetBundleOptions.CollectDependencies 包含資源的依賴關(guān)系,比如要導出的資源是一個PRefab,就會把其引用的mesh、貼圖、材質(zhì)球、動畫統(tǒng)統(tǒng)導出到資源包里面去//BuildTarget.Android 導出的資源包是給什么平臺用的,Android、PC、或者別的什么
?、诙鄠€資源導出成一個assetbundle;
BuildPipeline.BuildAssetBundle(null, Object[] objs, string path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);//objs 包含多個資源的數(shù)組,Object[]類型//path 路徑(例如:"Assets/streamingassets/obj.unity3d"),資源包的后綴名可以任意寫或不寫//BuildAssetBundleOptions.CollectDependencies 包含資源的依賴關(guān)系,比如要導出的資源是一個prefab,就會把其引用的mesh、貼圖、材質(zhì)球、動畫統(tǒng)統(tǒng)導出到資源包里面去//BuildTarget.Android 導出的資源包是給什么平臺用的,Android、PC、或者別的什么
2、讀取assetbundle:
①加載到內(nèi)存;
?、诮鈮簽榫唧w資源。
IEnumerator Load(string path, string name){ /*下面是加載到內(nèi)存的過程*/ WWW bundle = new WWW(path); //例如:"Assets/streamingassets/obj.unity3d" yield return bundle; /*下面是解壓為具體資源的過程*/ Object obj = bundle.assetBundle.Load(name); //name是具體資源的名字 }
注:IEnumerator是協(xié)程,可以想象成Unity3d中的多線程,就是同時可以干很多事情的意思,雖然原理上與多線程不同,但使用起來的效果幾乎沒有區(qū)別。如果要處理的是很多資源的assetbundle的話,不管是導成一個了,還是多個的,要結(jié)合策劃的具體需求合理規(guī)劃協(xié)程的組織。
隨手補充:
[MenuItem("Assets/Export Android Assetbundle")]static void Export(){ string fileName = Selection.activeObject.name; string assetbundlePath = EditorUtility.SaveFilePanel("Save", application.dataPath, fileName, "assetbundle"); BuildPipeline.BuildAssetBundle(Selection.activeObject, null, assetbundlePath, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android); }
新聞熱點
疑難解答