unity學習 5.x依賴打包和解包


unity5已經封裝好了接口,所以依賴打包並沒有那么神秘和復雜了。


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM