創建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
下面通過一個示例演示如何加載AssetBundle:
[csharp] view plaincopy
<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依賴關係
如果一個公共對象被多個對象依賴,我們打包的時候,可以有兩種選取。一種是比較省事的,就是將這個公共對象打包到每個對象中。這樣會有很多弊端:內存被浪費了;加入公共對象改變了,每個依賴對象都得重新打包。AssetBundle提供了依賴關係打包。
[csharp] view plaincopy
<span style="font-family:Microsoft YaHei;"> //啓用交叉引用,用於所有跟隨的資源包文件,直到我們調用PopAssetDependencies
BuildPipeline.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>