extractor?
創(chuàng)建AssetBundle1.創(chuàng)建一個(gè)空的Prefab,命名Cube,然后創(chuàng)建一個(gè)Cube,將其拉到剛創(chuàng)建好的Prefab2.新建一個(gè)腳本ExportAssetBundles.cs(代碼來(lái)自官方文檔),保存在Asset/Editor目錄下[csharp]viewplaincopyprint?//在Unity編輯器中添加菜單[MenuItem("Assets/BuildAssetBundleFromSelection")]staticvoidExportResourceRGB2(){//打開(kāi)保存面板,獲得用戶(hù)選擇的路徑stringpath=EditorUtility.SaveFilePanel("SaveResource","","NewResource","assetbundle");if(path.Length!=0){//選擇的要保存的對(duì)象Object[]selection=Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);//打包BuildPipeline.BuildAssetBundle(Selection.activeObject,selection,path,BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets,BuildTarget.StandaloneWindows);}}這時(shí)我們將看到Asset下面出現(xiàn)BuildAssetBundleFromSelection和BuildScene3.選中預(yù)設(shè)Cube,運(yùn)行BuildAssetBundleFromSelection。這時(shí)會(huì)彈出一個(gè)保存框,將其命名為cube.unity3d(這里為了測(cè)試方便,放在c盤(pán)。實(shí)際項(xiàng)目中,我們是需要將他們放在web服務(wù)器,供所有客戶(hù)端下載更新)4.新建一個(gè)場(chǎng)景scene1.unity,上面放置幾個(gè)模型,然后保存5.選中該場(chǎng)景,在之前的ExportAssetBundles.cs腳本中添加打包場(chǎng)景的函數(shù),運(yùn)行Assets->BuildScene,保存為scene1.unity3d(這里為了測(cè)試方便,也放在c盤(pán))[csharp]viewplaincopyprint?[MenuItem("Assets/SaveScene")]staticvoidExportScene(){//打開(kāi)保存面板,獲得用戶(hù)選擇的路徑stringpath=EditorUtility.SaveFilePanel("SaveResource","","NewResource","unity3d");if(path.Length!=0){//選擇的要保存的對(duì)象Object[]selection=Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);string[]scenes={"Assets/scene1.unity"};//打包BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);}}注意事項(xiàng)a.AssetBundle的保存后綴名可以是assetbundle或者unity3db.BuildAssetBundle要根據(jù)不同的平臺(tái)單獨(dú)打包,BuildTarget參數(shù)指定平臺(tái),如果不指定,默認(rèn)的webplayer加載AssetBundle我們通過(guò)一個(gè)簡(jiǎn)單的代碼來(lái)演示如何加載assetbundle,包括加載普通asset和場(chǎng)景。[csharp]viewplaincopyprint?usingSystem;usingUnityEngine;usingSystem.Collections;publicclassLoad:MonoBehaviour{privatestringBundleURL="file:///C:/cube.assetbundle";privatestringSceneURL="file:///C:/scene1.unity3d";voidStart(){//BundleURL="file//"+Application.dataPath+"/cube.assetbundle";Debug.Log(BundleURL);StartCoroutine(DownloadAssetAndScene());}IEnumeratorDownloadAssetAndScene(){//下載assetbundle,加載Cubeusing(WWWasset=newWWW(BundleURL)){yieldreturnasset;AssetBundlebundle=asset.assetBundle;Instantiate(bundle.Load("Cube"));bundle.Unload(false);yieldreturnnewWaitForSeconds(5);}//下載場(chǎng)景,加載場(chǎng)景using(WWWscene=newWWW(SceneURL)){yieldreturnscene;AssetBundlebundle=scene.assetBundle;Application.LoadLevel("scene1");}}}注意事項(xiàng)a.LoadFromCacheOrDownload可以指定版本,如果本地版本是新的,將不會(huì)從服務(wù)器讀取b.如果是多個(gè)資源打包在一起,我們要通過(guò)bundle.Load(),加載特定的資源c.掛載在模型上的腳本也可以一起打包,但是保證腳本在原目錄也要存在,否則加載出來(lái)無(wú)法運(yùn)行。關(guān)于如何更新腳本,我將放在以后的章節(jié)中闡述。AssetBundle依賴(lài)關(guān)系如果一個(gè)公共對(duì)象被多個(gè)對(duì)象依賴(lài),我們打包的時(shí)候,可以有兩種選取。一種是比較省事的,就是將這個(gè)公共對(duì)象打包到每個(gè)對(duì)象中。這樣會(huì)有很多弊端:內(nèi)存被浪費(fèi)了;加入公共對(duì)象改變了,每個(gè)依賴(lài)對(duì)象都得重新打包。AssetBundle提供了依賴(lài)關(guān)系打包。我們通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)學(xué)習(xí)[csharp]viewplaincopyprint?//啟用交叉引用,用于所有跟隨的資源包文件,直到我們調(diào)用PopAssetDependenciesBuildPipeline.PushAssetDependencies();varoptions=BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets;//所有后續(xù)資源將共享這一資源包中的內(nèi)容,由你來(lái)確保共享的資源包是否在其他資源載入之前載入BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),null,"Shared.unity3d",options);//這個(gè)文件將共享這些資源,但是后續(xù)的資源包將無(wú)法繼續(xù)共享它BuildPipeline.PushAssetDependencies();BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),null,"Lerpz.unity3d",options);BuildPipeline.PopAssetDependencies();這個(gè)文件將共享這些資源,但是后續(xù)的資源包將無(wú)法繼續(xù)共享它BuildPipeline.PushAssetDependencies();BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosiveguitex.prefab"),null,"explosive.unity3d",options);BuildPipeline.PopAssetDependencies();BuildPipeline.PopAssetDependencies();我們?cè)诔绦蚣虞d的時(shí)候必須保證先加載公共對(duì)象。否則,只能是在各個(gè)對(duì)象加載成功后,再通過(guò)程序手動(dòng)添加進(jìn)來(lái),比較繁瑣。在實(shí)際項(xiàng)目中,由于是團(tuán)隊(duì)開(kāi)發(fā),對(duì)象間的依賴(lài)關(guān)系通常會(huì)比較凌亂,最好在開(kāi)發(fā)周期就定好相關(guān)的規(guī)范約束,方便管理。