生活随笔
收集整理的這篇文章主要介紹了
实力封装: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."); } ?
[cpp]?view plaincopy
?? ?
?
按↑圖操作,就可以打包選中的文件了。
?
更多花樣,現在你可以自己創造了。今天就到這兒啦,咱們下期見~
轉載于:https://www.cnblogs.com/huwenya/p/9246229.html
總結
以上是生活随笔為你收集整理的实力封装:Unity打包AssetBundle(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。