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