创建AssetBundle

1.新建一个cube,将其拉倒Project视图里创建预设。

2.在Assets目录下创建Scenes文件夹,创建场景scene1.unity。

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


[csharp] view plaincopyUnity3d热更新(二):资源打包AssetBundle_新建Unity3d热更新(二):资源打包AssetBundle_资源_02

  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 plaincopyUnity3d热更新(二):资源打包AssetBundle_新建Unity3d热更新(二):资源打包AssetBundle_资源_02

  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 plaincopyUnity3d热更新(二):资源打包AssetBundle_新建Unity3d热更新(二):资源打包AssetBundle_资源_02

  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>