1. 程式人生 > >實力封裝:Unity打包AssetBundle(二)

實力封裝:Unity打包AssetBundle(二)

第二種打包方式

Unity提供的BuildAssetBundles API還有一個過載形式,看下面↓↓

public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

這個過載函式多了一個引數,這個引數是一個AssetBundleBuild陣列,下面我們來說說AssetBundleBuild是何方妖孽↓↓

AssetBundleBuild是一個結構體,有四個屬性,說明如下↓↓

assetBundleName
string型別,打包後AssetBundle包的名字
assetNames
string陣列型別,AssetBundle包中每一個檔案相對於工程目錄的路徑(一個AssetBundle包中可以有多個檔案)
addressableNames
string陣列型別,相當於是assetNames的別名,必須和assetNames長度一致
assetBundleVariant
string型別,設定AssetBundle變體,其實就是字尾名

一般情況下,只要設定AssetBundleBuild結構體中的assetBundleName屬性和assetNames屬性就可以了,如果你願意給打在AssetBundle包中的檔案重新命名的話,就設定一下addressableNames屬性,不想重新命名的檔案就給一個空字串,這樣預設就會使用assetNames了。要是還想設定自定義的字尾名,可以試試AssetBundleVariant。

有了AssetBundleBuild這樣一個結構體陣列做引數,這就意味著我們可以讓打包的過程更簡單了。不用在Inspector面板中給每一個需要打包的檔案設定AssetBundle名稱,我們可以直接在編輯器中滑鼠點選需要打包的檔案,然後一鍵打包。當然,這也就意味著我們不能隨意的控制每個AssetBundle包的名字了,有利有弊吧。

在這裡講一講打包的方式吧,我們可以把許多檔案打進一個AssetBundle包,也可以將每個檔案單獨打一個AssetBundle包,具體怎麼打就要看需求了。

下面是打包的程式碼↓↓

[MenuItem("AssetBundles/Buil (All)")]
private static void _packageBuddlesInOne() {
	string _packagePath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "F:/", "");
	if (_packagePath.Length <= 0 || !Directory.Exists (_packagePath))
		return;
	buildMap[0].assetBundleName = "myBnndle";
	//將選中物件一起打包
	AssertBundleAssetBundleBuild[] buildMap = new AssetBundleBuild[1];
	GameObject[] objs = Selection.gameObjects; //獲取當前選中的所有物件
	string[] itemAssets = new string[objs.Length];
	for (int i = 0; i < objs.Length; i++) {
		itemAssets [i] = AssetDatabase.GetAssetPath (objs [i]); //獲取物件在工程目錄下的相對路徑
	}
	buildMap [0].assetNames = itemAssets;
	AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles (_packagePath, buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
	AssetDatabase.Refresh (); //重新整理
	if (manifest == null)
		Debug.LogError("Package AssetBundles Faild.");
	else
		Debug.Log("Package AssetBundles Success.");
}
這樣所有你選中的物件就都打進一個AssetBundle包了,建議關聯或相似的檔案可以這樣打包。

我們也可以將每個物件都打進不同的AssetBundle包,看程式碼↓↓

[MenuItem("AssetBundle/Build (Selected)")]
private static void _packageBuddleSelected() {
	string _packagePath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "F:/", "");
	if (_packagePath.Length <= 0 || !Directory.Exists (_packagePath))
		return;
	AssetBundleBuild[] buildMap = new AssetBundleBuild[objs.Length];
	GameObject[] objs = Selection.gameObjects;
	for (int i = 0; i < objs.Length; i++) {
		string[] itemAsset = new string[] { AssetDatabase.GetAssetPath (objs[i]) };
		buildMap[i].assetBundleName = objs[i].name;
		buildMap [i].assetNames = itemAsset;
	}
	AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles (_packagePath, buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
	AssetDatabase.Refresh ();
	if(menifest == null)
		Debug.LogError("Error:Package Failed");
	else
		Debug.Log("Package Success.");
}

按↑圖操作,就可以打包選中的檔案了。

更多花樣,現在你可以自己創造了。今天就到這兒啦,咱們下期見~