一、創建Assetbundle
在unity3d開發的游戲中,無論模型,音頻,還是圖片等,我們都做成Prefab,然后打包成Assetbundle,方便我們后面的使用,來達到資源的更新。
? ? ? ?一個Assetbundle可以打包一個模型(這里的模型不單單指的是預制模型,可以是Project視圖下的任何東西),也可以是多個模型,但兩種打包方式占用的空間不一樣。
? ? ? 比如我打包三個一樣的模型(只不過他們的腳本不一樣創建三個空的GameObject(One,Two,Three),分別掛載腳本One,Two,Three)。如果我為每個模型單獨打包生成One,Two,Three三個Assetbundle,其所占的空間是A,B,C,但是A+B+C != D.由此可知想通的資源盡可能的打包到一起,他們共用一套資源。不相同的模型盡量分開打包。


二、分開打包(注意這個腳本必須放在Editor文件夾內,Editor文件夾沒有的話需自己創建)
- ??
- ??
- ??
- [MenuItem("AssetBundleDemo/Create?AssetBundles?By?themselves")]??
- static?void?CreateAssetBundleThemelves(){??
- ??????
- ????Object[]?selects?=?Selection.GetFiltered?(typeof(Object),SelectionMode.DeepAssets);??
- ??????
- ????foreach(Object?obj?in?selects){??
- ??????????
- ??????????
- ??????????
- ????????string?targetPath?=?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/"?+?obj.name?+?".assetbundle";??
- ????????if(BuildPipeline.BuildAssetBundle(obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies)){??
- ??
- ????????????Debug.Log(obj.name?+?"is?packed?successfully!");??
- ????????}else{??
- ????????????Debug.Log(obj.name?+?"is?packed?failly!");??
- ????????}??
- ????}??
- ??????
- ????AssetDatabase.Refresh?();??
- }??
SelectionMode.DeepAssets
這個選擇模式意味著如果選擇中包含多個文件,那么他將包含這個文件視圖中的所有資源。
他還有其他的各種選項(以下是官方文檔)
SelectionMode
Description
SelectionMode can be used to tweak the selection returned by Selection.GetTransforms.
The default transform selection mode is: SelectionMode.TopLevel | SelectionMode.ExcludePrefab | SelectionMode.Editable.
Unfiltered | Return the whole selection. |
TopLevel | Only return the topmost selected transform. A selected child of another selected transform will be filtered out. |
Deep | Return the selection and all child transforms of the selection. |
ExcludePrefab | Excludes any prefabs from the selection. |
Editable | Excludes any objects which shall not be modified. |
Assets | Only return objects that are assets in the Asset directory. |
DeepAssets | If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy. |
最核心的方法:BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)
參數1:它只能放一個對象,因為我們這里是分別打包,所以通過循環將每個對象分別放在了這里。
參數2:可以放入一個數組對象。
參數3:要打包到的路徑
參數4:默認情況下打的包只能在電腦上用,如果要在手機上用就要添加一個參數。
Android上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)
IOS上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.iPhone)
另外,電腦上和手機上打出來的Assetbundle不能混用,不同平臺只能用自己的。
三、一起打包
- ??
- ??
- ??
- [MenuItem("AssetBundleDemo/Create?AssetBundles?Together")]??
- static?void?CreateAssetBundleTogether(){??
- ??????
- ????Object[]?selects?=?Selection.GetFiltered?(typeof(Object),SelectionMode.DeepAssets);??
- ??????
- ????string?targetPath?=?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/Together.assetbundle";??
- ????if(BuildPipeline.BuildAssetBundle(null,selects,targetPath,BuildAssetBundleOptions.CollectDependencies)){??
- ????????Debug.Log("Packed?successfully!");??
- ??
- ????}else{??
- ????????Debug.Log("Packed?failly!");??
- ????}??
- ??????
- ????AssetDatabase.Refresh?();??
- }??
四、讀取
- using?UnityEngine;??
- using?System.Collections;??
- ??
- public?class?ReanAssetbundle?:?MonoBehaviour?{??
- ??
- ??????
- ????public?static?readonly?string?m_PathURL?=??
- ????????#if?UNITY_ANDROID??
- ????????"jar:file://"?+?Application.dataPath?+?"!/assets/";??
- ????????#elif?UNITY_IPHONE??
- ????????Application.dataPath?+?"/Raw/";??
- ????????#elif?UNITY_STANDALONE_WIN?||?UNITY_EDITOR??
- ????????"file://"?+?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/";??
- ????????#else??
- ????????string.Empty;??
- ????????#endif??
- ??
- ????void?OnGUI(){??
- ????????if(GUILayout.Button("加載分開打包的Assetbundle")){??
- ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+?"One.assetbundle"));??
- ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+??"Two.assetbundle"));??
- ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+?"Three.assetbundle"));??
- ??
- ????????}??
- ??????????
- ????????if(GUILayout.Button("加載打包在一起的Assetbundle")){??
- ????????????StartCoroutine(LoadGameObjectPackedTogether(m_PathURL?+?"Together.assetbundle"));??
- ????????}??
- ??????????
- ????}??
- ??????
- ????private?IEnumerator?LoadGameObjectPackedByThemselves(string?path){??
- ????????WWW?bundle?=?new?WWW?(path);??
- ????????yield?return?bundle;??
- ??
- ??????????
- ????????yield?return?Instantiate?(bundle.assetBundle.mainAsset);??
- ????????bundle.assetBundle.Unload?(false);??
- ????}??
- ??
- ????IEnumerator??LoadGameObjectPackedTogether?(string?path)??
- ????{??
- ????????WWW?bundle?=?new?WWW?(path);??
- ????????yield?return?bundle;??
- ??
- ????????Object?one?=?bundle.assetBundle.Load?("One");??
- ????????Object?two?=?bundle.assetBundle.Load?("Two");??
- ????????Object?three?=?bundle.assetBundle.Load?("Three");??
- ??
- ??????????
- ????????yield?return?Instantiate?(one);??
- ????????yield?return?Instantiate?(two);??
- ????????yield?return?Instantiate?(three);??
- ????????bundle.assetBundle.Unload?(false);??
- ????}??
- }??