Unity熱更新學習(一) —— AssetBundle 打包和加載


理論不多說,網上,官方文檔都有。  這里有一篇介紹很全面的文章:https://www.cnblogs.com/ybgame/p/3973177.html

示例和注意點記錄一下,用到時以便查閱。

 

一、打包代碼(Editor)

我所知道的,有兩種打包方法:1、所有打包參數在代碼里設置,完全代碼控制;2、在編輯器中設置打包參數,代碼簡便

第一種:

在代碼中指定bundle包名,指定包含在該bundle里的所有資源路徑

 1 [MenuItem("AssetsBundle/Build Bundle -- 代碼中設置參數")]
 2     static void BuildAB()
 3     {
 4         string path = Application.streamingAssetsPath + "/AssetsBundles/";
 5 
 6         if (Directory.Exists(path))
 7             Directory.Delete(path, true);
 8         Directory.CreateDirectory(path);
 9 
10         AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
11 
12         //指定bundle包名
13         buildMap[0].assetBundleName = "prefab_bundle";
14 
15         string[] assets = new string[2];
16         assets[0] = "Assets/AssetsBundleObj/Cube.prefab"; // 必須是完整的文件名(包括后綴)
17         assets[1] = "Assets/AssetsBundleObj/Sphere.prefab";
18         //指定bundle包含的資源路徑數組
19         buildMap[0].assetNames = assets;
20 
21         buildMap[1].assetBundleName = "scene_bundle";
22         string[] scenes = new string[1];
23         scenes[0] = "Assets/_Scenes/Scene.unity";
24         buildMap[1].assetNames = scenes;
25 
26         BuildPipeline.BuildAssetBundles(path, buildMap, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
27     }
代碼中設置參數

第二種:

先在編輯器中設置參數,選中要打包的對象,設置好包名,后綴。

 

然后就直接打bundle包了,無需再在代碼中處理包名之類的參數。

 1 [MenuItem("AssetsBundle/Build Bundle -- 編輯器中設置參數")]
 2     static void BuildAB2()
 3     {
 4         string path = Application.streamingAssetsPath + "/AssetsBundles/";
 5 
 6         if (Directory.Exists(path))
 7             Directory.Delete(path, true);
 8         Directory.CreateDirectory(path);
 9 
10         BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);
11     }
編輯器中設置參數

 

二、加載代碼(runtime)

通過www方法加載bundle,並把加載出來的資源實例化。

 1 void OnGUI()  2  {  3         if (GUILayout.Button("加載預制體Cube"))  4  {  5             StartCoroutine(LoadObj("prefab_bundle", "cube.prefab"));//有沒有.prefab后綴都正常加載
 6  }  7         if (GUILayout.Button("加載預制體Sphere"))  8  {  9             StartCoroutine(LoadObj("prefab_bundle", "sphere")); 10  } 11  } 12     /// <summary>
13     /// 加載預制體 14     /// </summary>
15     /// <param name="name"></param>
16     /// <returns></returns>
17     IEnumerator LoadObj(string bundle_name, string name) 18  { 19         string path = "file://" + Application.streamingAssetsPath + "/AssetsBundles/" + bundle_name; 20         //Debug.LogError(string.Format("obj {0}", path));
21         WWW www = new WWW(path); 22         yield return www; 23         if (www.error == null) 24  { 25             AssetBundle bundle = www.assetBundle; 26             //這里LoadAsset第二個參數 有沒有都能正常運行,這個類型到底什么用途還有待研究
27             GameObject go = Instantiate(bundle.LoadAsset(name, typeof(GameObject))) as GameObject; 28             //go.transform.parent = transform; 29             // 上一次加載完之后,下一次加載之前,必須卸載AssetBundle,不然再次加載報錯: 30             // The AssetBundle 'Memory' can't be loaded because another AssetBundle with the same files is already loaded
31             bundle.Unload(false); 32         }else
33  { 34  Debug.LogError(www.error); 35  } 36     }
加載預制體測試

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM