Unity3d熱更新(二):資源打包As
- 2020 年 1 月 11 日
- 筆記
創建AssetBundle
1.新建一個cube,將其拉倒Project視圖裡創建預設。
2.在Assets目錄下創建Scenes文件夾,創建場景scene1.unity。
3.新建ExportAssetBundles.cs,保存在Assets/Editor目錄下。程式碼如下:
[csharp] view plaincopy
- <span style="font-family:Microsoft YaHei;">using UnityEngine;
- using UnityEditor;
- using System.Collections;
- public class ExportAssetBundles : MonoBehaviour {
- [MenuItem("Build/ExportResource")]
- static void ExportResource()
- {
- // 打開保存面板,獲取用戶選擇的路徑
- string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "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);
- }
- }
- [MenuItem("Build/ExportScene")]
- static void ExportScene()
- {
- // 打開面板,選擇用戶保存的路徑
- string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
- if (path.Length != 0)
- {
- // 要打包的場景
- string[] scenes = {"Assets/Scenes/scene1.unity"};
- // 打包
- BuildPipeline.BuildPlayer(scenes, path, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes);
- }
- }
- }</span>
4.選中預設,運行ExportResource,彈出保存對話框,命名為cube.assetbundle。
5.運行ExportScene,彈出保存對話框,命名為scene1.unity3d。
小提示
1.AssetBundle的保存後綴名可以是assetbundle或者unity3d。
2.BuildAssetBundle要根據不同的平台單獨打包,BuildTarget參數指定平台,如果不指定,默認的webplayer。
載入AssetBundle
- <span style="font-family:Microsoft YaHei;">using UnityEngine;
- using System.Collections;
- public class Load : MonoBehaviour {
- private string BundleUrl = "file:///C:/Users/Administrator/Desktop/Res/cube.assetbundle";
- private string SceneUrl = "file:///C:/Users/Administrator/Desktop/Res/scene1.unity3d";
- void Start()
- {
- StartCoroutine(Download());
- }
- IEnumerator Download()
- {
- // 下載AssetBundle,載入cube
- using(WWW www = new WWW(BundleUrl))
- {
- yield return www;
- AssetBundle bundle = www.assetBundle;
- Instantiate(bundle.Load("Cube"));
- bundle.Unload(false);
- yield return new WaitForSeconds(5);
- }
- using(WWW www = new WWW(SceneUrl))
- {
- yield return www;
- Application.LoadLevel("scene1");
- }
- }
- }</span>
我們在程式載入的時候必須保證先載入公共對象。否則,只能是在各個對象載入成功後,再通過程式手動添加進來,比較繁瑣。在實際項目中,由於是團隊開發,對象間的依賴關係通常會比較凌亂,最好在開發周期就定好相關的規範約束,方便管理。
AssetBundle依賴關係
<span style="font-family:Microsoft YaHei;"> //啟用交叉引用,用於所有跟隨的資源包文件,直到我們調用PopAssetDependenciesBuildPipeline.PushAssetDependencies();var options =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/explosive guitex.prefab"),null, "explosive.unity3d", options);BuildPipeline.PopAssetDependencies();BuildPipeline.PopAssetDependencies(); </span>