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

  • 2020 年 1 月 11 日
  • 筆記

創建AssetBundle

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

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

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

[csharp] view plaincopy

  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

  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

  1. <span style="font-family:Microsoft YaHei;">    //啟用交叉引用,用於所有跟隨的資源包文件,直到我們調用PopAssetDependencies  
  2.     BuildPipeline.PushAssetDependencies();    
  3.     var options =    
  4.         BuildAssetBundleOptions.CollectDependencies |    
  5.         BuildAssetBundleOptions.CompleteAssets;    
  6. //所有後續資源將共享這一資源包中的內容,由你來確保共享的資源包是否在其他資源載入之前載入  
  7.     BuildPipeline.BuildAssetBundle(    
  8.         AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),    
  9. null, "Shared.unity3d", options);    
  10. //這個文件將共享這些資源,但是後續的資源包將無法繼續共享它  
  11.     BuildPipeline.PushAssetDependencies();    
  12.     BuildPipeline.BuildAssetBundle(    
  13.         AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),    
  14. null, "Lerpz.unity3d", options);    
  15.     BuildPipeline.PopAssetDependencies();  
  16. //這個文件將共享這些資源,但是後續的資源包將無法繼續共享它  
  17.     BuildPipeline.PushAssetDependencies();    
  18.     BuildPipeline.BuildAssetBundle(    
  19.         AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"),    
  20. null, "explosive.unity3d", options);    
  21.     BuildPipeline.PopAssetDependencies();   
  22.     BuildPipeline.PopAssetDependencies();  </span>  

Tags: