AssetBundle打包和加載

 好吧!廢話不多講,直接上圖上操作哈!
  1.首先還是新建一個unity3d項目,導入需要資源文件,我這邊導入一個asset store 模型插件 unity-chan資源。
  2.第一步,我們先創建打包AssetBundle 的環境的,項目需要一個內置的文件夾Editor (這是必須操作,這是爲了可以在項目編輯器中打包AssetBundle資源),並且附上AssetBundleEditor.cs組件類。  需要注意的是 :不同的平臺參數是不一樣的,IOS平臺上Assetbundle打包時需要使用 BuildTarget.iPhone 參數。
 

 

  AssetBundleEditor.cs 代碼如下:

[csharp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4. using System.IO;  
  5.    
  6. public class AssetBundleEditor : Editor   
  7. {  
  8.    
  9.         public static string sourcePath = Application.dataPath + "/Resource";  
  10.         private static string AssetBundlesOutputPath = Application.dataPath + "/StreamingAssets";  
  11.    
  12.         [MenuItem("AssetBundles/BuildAssetBundle")]  
  13.         public static void BuildAssetBundle()  
  14.         {  
  15.                 string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(BuildTarget.iOS));  
  16.                 if (!Directory.Exists(outputPath))  
  17.                 {  
  18.                         Directory.CreateDirectory(outputPath);  
  19.                 }  
  20.    
  21.                 //根據BuildSetting裏面所激活的平臺進行打包 設置過AssetBundleName的都會進行打包  
  22.                 BuildPipeline.BuildAssetBundles("Assets/StreamingAssets/IOS",BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS);  
  23.    
  24.                 AssetDatabase.Refresh();  
  25.    
  26.                 Debug.Log("打包完成");  
  27.    
  28.         }  
  29. }  
  30.    
  31. public class Platform  
  32. {  
  33.         public static string GetPlatformFolder(BuildTarget target)  
  34.         {  
  35.                 switch (target)  
  36.                 {  
  37.                 case BuildTarget.Android:  
  38.                         return "Android";  
  39.                 case BuildTarget.iOS:  
  40.                         return "IOS";  
  41.                 case BuildTarget.StandaloneWindows:  
  42.                 case BuildTarget.StandaloneWindows64:  
  43.                         return "Windows";  
  44.                 case BuildTarget.StandaloneOSXIntel:  
  45.                 case BuildTarget.StandaloneOSXIntel64:  
  46.                 case BuildTarget.StandaloneOSXUniversal:  
  47.                         return "OSX";  
  48.                 default:  
  49.                         return null;  
  50.                 }  
  51.         }  
  52. }  




  3.第二步,我們把打包好的AssetBundle資源解包出來,現在在場景中新建一個LoadAssetBundle的對象,實現加載asset bundle資源的腳本。
  LoadAssetBundle.cs 代碼如下: 

[csharp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4.    
  5. public class LoadAssetBundle : MonoBehaviour  
  6. {  
  7.    
  8.         public  string PathURL;  
  9.         public GameObject CurrentObj;  
  10.    
  11.         void Start()  
  12.         {  
  13.                 #if UNITY_IPHONE    
  14.                 PathURL = "file://"+Application.dataPath + "/Raw/IOS";    
  15.                 #elif UNITY_STANDALONE_WIN || UNITY_EDITOR    
  16.                 PathURL = "file://"+Application.streamingAssetsPath+"/IOS";  
  17.                 #endif    
  18.    
  19.         }  
  20.    
  21.         void OnGUI()  
  22.         {  
  23.                 if(GUI.Button(new Rect(100,100,100,100),"加載美少女"))  
  24.                 {  
  25.                         StartCoroutine (GetAssetBundleObj("girl_001",PathURL));  
  26.                 }  
  27.         }  
  28.    
  29.         public  IEnumerator GetAssetBundleObj(string objName,string path="")  
  30.         {  
  31.                 string filePath = System.IO.Path.Combine (path,objName);  
  32.    
  33.                 WWW w = new WWW(filePath);         //利用www類加載  
  34.                 yield return w;  
  35.                 AssetBundle curBundleObj = w.assetBundle;     //獲得AssetBundle  
  36.    
  37.                 AssetBundleRequest obj = curBundleObj.LoadAssetAsync(objName, typeof(GameObject));    //異步加載GameObject類型  
  38.                 yield return obj;  
  39.                 CurrentObj = Instantiate(obj.asset) as GameObject;  
  40.    
  41.                 yield return null;  
  42.                 curBundleObj.Unload(false);     //卸載所有包含在bundle中的對象,已經加載的纔會卸載  
  43.                 w.Dispose();  
  44.         }  
  45.    
  46. }  



   
  4.其實從運行項目的時候,我們會發現模型的加載不正常,因爲它丟失了材質,而我們找到的解決的方法是在Edit->Project Setting->Graphics 設置中的Always Include Shaders 中增加模型的Shader,Shader配置好以後,記得重新打包模型。
 

 

 


5.好吧,接下來我們Build一個版本在真機上,這個涉及到 IOS 開發部署流程,有興趣的同學可以去研究一下,然後,我在這邊直接操作啦!
  
5.0 首先BuildSetting 切換平臺到IOS;
 
  
5.1 直接在真機上的效果。
 
最後,我們來看一下www訪問方式:
(1).非緩存方式:
WWW w=new WWW(url:string);
通過創建一個WWW實例來對AssetBundle文件下載,下載後的AssetBundle文件將不會進入Unity的緩存區。
   

(2)緩存方式:

WWW w=WWW.LoadFromCacheOrDownload(url:string,version:int);


下載後的AssetBundle會自動被保存到Unity引擎的緩存區內,該方法是Unity推薦的AssetBundle下載方式。下載AssetBundle的時候,該接口會先在本地緩存中查找該文件,看其之前是否被下載過,如果下載過,則直接從緩存中加載,如果沒有,則從服務器盡享下載。這樣做的好處是可以節省AssetBundle文件的下載時間,從而提高遊戲資源的載入速度(還可以節省下載流量)。
  
        注意:但是WWW.LoadFromCacheOrDownload(url:string,version:int)在測試情況下你可能會頻繁的打包生成Assetbundle,如果忘記改版本號的話可能會讀取之前的緩存,可能就會看不到新的效果,所以建議在bunild Assetbundle的時候強制清空一下緩存。
      Caching.CleanCache(); 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章