unity5已經封裝好了接口,所以依賴打包並沒有那么神秘和復雜了。
打包:
1.定義好資源的assetBundleName
2.BuildPipeline.BuildAssetBundles,指定資源目錄和壓縮類型
生成:
1.Assetbundle文件,加載時的首要文件,包含所有資源的依賴信息
2.每個文件對應一個.manifest,不用管他,但是可以打開查看他引用了哪些資源。
加載:
1.獲取AssetBundle文件
2.LoadAsset("AssetBundleManifest")轉換為AssetBundleManifest
3.通過manifest.GetAllDependencies("測試文件"),獲取它依賴的ab,得到的是AB數組,並下載它
4.最后下載名為(測試文件)的資源即可。
測試代碼:
- using UnityEngine;
- using System.Collections;
- public class LoadAssetbundle : MonoBehaviour {
- void Start()
- {
- // 1.加載Manifest文件
- AssetBundle manifestBundle=AssetBundle.CreateFromFile(Application.dataPath +"/ab/Assetbundle");
- if(manifestBundle != null)
- {
- AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
- // 2.獲取依賴文件列表
- string[] cubedepends = manifest.GetAllDependencies("assets/res/1.prefab");
- AssetBundle[] dependsAssetbundle = new AssetBundle[cubedepends.Length];
- for(int index = 0; index < cubedepends.Length; index++)
- {
- // 3.加載所有的依賴資源
- dependsAssetbundle[index]=AssetBundle.CreateFromFile(
- Application.dataPath +"/../Assetbundle/"+cubedepends[index]);
- }
- // 4.加載資源
- AssetBundle cubeBundle=AssetBundle.CreateFromFile(
- Application.dataPath +"/ab/assets/res/1.prefab" );
- GameObject cube=cubeBundle.LoadAsset("1") as GameObject;
- if(cube!=null)
- {
- Instantiate(cube);
- }
- }
- }
- }
坑tips:
如果材質球的名稱和它引用的貼圖名稱一樣,材質球內存占有量就會像包含了貼圖的內存一樣。
換版本tips:
4.6項目移植到5.0.2,ab包無法加載,重新打包即可。
不改打包代碼,多數文件大小增大2k
改為最新打包代碼,不做依賴,多數文件大小增大2-4%左右
tips:
如果做依賴,會生成很多零碎的文件,開發期不建議使用,以免增加不必要的工作量。和美術定義好資源規范才是重點。(個人意見,不喜隨你便)