Unity3d熱更新(二):資源打包AssetBundle

創建AssetBundle

1.新建一個cube,將其拉倒Project視圖裏創建預設。

2.在Assets目錄下創建Scenes文件夾,創建場景scene1.unity。

3.新建ExportAssetBundles.cs,保存在Assets/Editor目錄下。代碼如下:


[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片

  1. <span style="font-family:Microsoft YaHei;">using UnityEngine;  

  2. using UnityEditor;  

  3. using System.Collections;  

  4. public class ExportAssetBundles : MonoBehaviour {  

  5.     [MenuItem("Build/ExportResource")]  

  6.     static void ExportResource()  

  7.     {  

  8.         // 打開保存面板,獲取用戶選擇的路徑  

  9.         string path = EditorUtility.SaveFilePanel("Save Resource""""New Resource""assetbundle");  

  10.         if (path.Length != 0)  

  11.         {  

  12.             // 選擇的要保存的對象  

  13.             Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);  

  14.             // 打包  

  15.             BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);  

  16.         }  

  17.     }  

  18.     [MenuItem("Build/ExportScene")]  

  19.     static void ExportScene()  

  20.     {  

  21.         // 打開面板,選擇用戶保存的路徑  

  22.         string path = EditorUtility.SaveFilePanel("Save Resource""""New Resource""unity3d");  

  23.         if (path.Length != 0)  

  24.         {  

  25.             // 要打包的場景  

  26.             string[] scenes = {"Assets/Scenes/scene1.unity"};  

  27.             // 打包  

  28.             BuildPipeline.BuildPlayer(scenes, path, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes);  

  29.         }  

  30.     }  

  31. }</span>  


4.選中預設,運行ExportResource,彈出保存對話框,命名爲cube.assetbundle。

5.運行ExportScene,彈出保存對話框,命名爲scene1.unity3d。

小提示

1.AssetBundle的保存後綴名可以是assetbundle或者unity3d。

2.BuildAssetBundle要根據不同的平臺單獨打包,BuildTarget參數指定平臺,如果不指定,默認的webplayer。

加載AssetBundle

下面通過一個示例演示如何加載AssetBundle:

[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片

  1. <span style="font-family:Microsoft YaHei;">using UnityEngine;  

  2. using System.Collections;  

  3. public class Load : MonoBehaviour {  

  4.     private string BundleUrl = "file:///C:/Users/Administrator/Desktop/Res/cube.assetbundle";  

  5.     private string SceneUrl = "file:///C:/Users/Administrator/Desktop/Res/scene1.unity3d";  

  6.     void Start()  

  7.     {  

  8.         StartCoroutine(Download());  

  9.     }  

  10.     IEnumerator Download()  

  11.     {  

  12.         // 下載AssetBundle,加載cube  

  13.         using(WWW www = new WWW(BundleUrl))  

  14.         {  

  15.             yield return www;  

  16.             AssetBundle bundle = www.assetBundle;  

  17.             Instantiate(bundle.Load("Cube"));  

  18.             bundle.Unload(false);  

  19.             yield return new WaitForSeconds(5);  

  20.         }  

  21.         using(WWW www = new WWW(SceneUrl))  

  22.         {  

  23.             yield return www;  

  24.             Application.LoadLevel("scene1");  

  25.         }  

  26.     }  

  27. }</span>  


我們在程序加載的時候必須保證先加載公共對象。否則,只能是在各個對象加載成功後,再通過程序手動添加進來,比較繁瑣。在實際項目中,由於是團隊開發,對象間的依賴關係通常會比較凌亂,最好在開發週期就定好相關的規範約束,方便管理。


AssetBundle依賴關係


如果一個公共對象被多個對象依賴,我們打包的時候,可以有兩種選取。一種是比較省事的,就是將這個公共對象打包到每個對象中。這樣會有很多弊端:內存被浪費了;加入公共對象改變了,每個依賴對象都得重新打包。AssetBundle提供了依賴關係打包。

[csharp] view plaincopy在CODE上查看代碼片派生到我的代碼片

  1. <span style="font-family:Microsoft YaHei;">    //啓用交叉引用,用於所有跟隨的資源包文件,直到我們調用PopAssetDependencies    

  2.     BuildPipeline.PushAssetDependencies();    

  3.     

  4.     var options =    

  5.         BuildAssetBundleOptions.CollectDependencies |    

  6.         BuildAssetBundleOptions.CompleteAssets;    

  7.     

  8.     

  9.     //所有後續資源將共享這一資源包中的內容,由你來確保共享的資源包是否在其他資源載入之前載入    

  10.     BuildPipeline.BuildAssetBundle(    

  11.         AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),    

  12.         null"Shared.unity3d", options);    

  13.     

  14.     

  15.     //這個文件將共享這些資源,但是後續的資源包將無法繼續共享它    

  16.     BuildPipeline.PushAssetDependencies();    

  17.     BuildPipeline.BuildAssetBundle(    

  18.         AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),    

  19.         null"Lerpz.unity3d", options);    

  20.     BuildPipeline.PopAssetDependencies();  

  21.     

  22.     

  23.     //這個文件將共享這些資源,但是後續的資源包將無法繼續共享它    

  24.     BuildPipeline.PushAssetDependencies();    

  25.     BuildPipeline.BuildAssetBundle(    

  26.         AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"),    

  27.         null"explosive.unity3d", options);    

  28.     BuildPipeline.PopAssetDependencies();    

  29.     

  30.     

  31.     BuildPipeline.PopAssetDependencies();  </span>  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章