打包Assetbundle并加载

来源:互联网 发布:深圳 原画 培训 知乎 编辑:程序博客网 时间:2024/04/27 18:02

转自:http://blog.csdn.net/dingxiaowei2013/article/details/17439887

由于我们要将模型资源放在远程的服务器端,但如果直接放fbx模型是不可以加载的,所以我们可以将fbx做成预设或者是直接将其打包成assetbundle格式的,然后通过www来加载获取。

1.首先要讲一下不同平台下的一个StreamingAssets路径,这是不同的。

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.         //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。  
  2.         public static readonly string PathURL =  
  3. #if UNITY_ANDROID   //安卓  
  4.         "jar:file://" + Application.dataPath + "!/assets/";  
  5. #elif UNITY_IPHONE  //iPhone  
  6.         Application.dataPath + "/Raw/";  
  7. #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  //windows平台和web平台  
  8.     "file://" + Application.dataPath + "/StreamingAssets/";  
  9. #else  
  10.         string.Empty;  
  11. #endif  

这就获取到了不同平台的一个路径,我们可以将打包的文件放在这些路径下,然后再从这路径去读取资源。

2.关于打包assetbundle的脚本

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4.   
  5. public class Test : Editor  
  6. {  
  7.     //打包单个  
  8.     [MenuItem("Custom Editor/Create AssetBunldes Main")]  
  9.     static void CreateAssetBunldesMain ()  
  10.     {  
  11.         //获取在Project视图中选择的所有游戏对象  
  12.         Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);  
  13.    
  14.         //遍历所有的游戏对象  
  15.         foreach (Object obj in SelectedAsset)   
  16.         {  
  17.             //本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径  
  18.             //StreamingAssets是只读路径,不能写入  
  19.             //服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。  
  20.             string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";  
  21.             if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {  
  22.                 Debug.Log(obj.name +"资源打包成功");  
  23.             }   
  24.             else   
  25.             {  
  26.                 Debug.Log(obj.name +"资源打包失败");  
  27.             }  
  28.         }  
  29.         //刷新编辑器  
  30.         AssetDatabase.Refresh ();     
  31.           
  32.     }  
  33.       
  34.     [MenuItem("Custom Editor/Create AssetBunldes ALL")]  
  35.     static void CreateAssetBunldesALL ()  
  36.     {  
  37.           
  38.         Caching.CleanCache ();  
  39.    
  40.   
  41.         string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";  
  42.    
  43.           
  44.         Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);  
  45.    
  46.         foreach (Object obj in SelectedAsset)   
  47.         {  
  48.             Debug.Log ("Create AssetBunldes name :" + obj);  
  49.         }  
  50.           
  51.         //这里注意第二个参数就行  
  52.         if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {  
  53.             AssetDatabase.Refresh ();  
  54.         } else {  
  55.               
  56.         }  
  57.     }  
  58.       
  59.     [MenuItem("Custom Editor/Create Scene")]  
  60.     static void CreateSceneALL ()  
  61.     {  
  62.         //清空一下缓存  
  63.         Caching.CleanCache();  
  64.         string Path = Application.dataPath + "/MyScene.unity3d";  
  65.         string  []levels = {"Assets/Level.unity"};  
  66.         //打包场景  
  67.         BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);  
  68.         AssetDatabase.Refresh ();  
  69.     }  
  70.       
  71. }  

前提要新手动创建一个StreamingAssets文件夹

3.读取资源,这里只举例从本地读取,跟从网络读取是一样的,可以参考官方文档:

http://game.ceeger.com/Script/WWW/WWW.html

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class RunScript : MonoBehaviour  
  5. {  
  6.       
  7.   
  8.         //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。  
  9.         public static readonly string PathURL =  
  10. #if UNITY_ANDROID  
  11.         "jar:file://" + Application.dataPath + "!/assets/";  
  12. #elif UNITY_IPHONE  
  13.         Application.dataPath + "/Raw/";  
  14. #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  
  15.     "file://" + Application.dataPath + "/StreamingAssets/";  
  16. #else  
  17.         string.Empty;  
  18. #endif  
  19.       
  20.     void OnGUI()  
  21.     {  
  22.         if(GUILayout.Button("Main Assetbundle"))  
  23.         {  
  24.             //StartCoroutine(LoadMainGameObject(PathURL + "Prefab0.assetbundle"));  
  25.             //StartCoroutine(LoadMainGameObject(PathURL +  "Prefab1.assetbundle"));  
  26.           
  27.             StartCoroutine(LoadMainCacheGameObject(PathURL + "Prefab0.assetbundle"));  
  28.             StartCoroutine(LoadMainCacheGameObject(PathURL +  "Prefab1.assetbundle"));  
  29.         }  
  30.           
  31.         if(GUILayout.Button("ALL Assetbundle"))  
  32.         {  
  33.             StartCoroutine(LoadALLGameObject(PathURL + "ALL.assetbundle"));  
  34.         }  
  35.           
  36.         if(GUILayout.Button("Open Scene"))  
  37.         {  
  38.             StartCoroutine(LoadScene());  
  39.         }  
  40.           
  41.     }  
  42.       
  43.     //读取一个资源  
  44.       
  45.     private IEnumerator LoadMainGameObject(string path)  
  46.     {  
  47.          WWW bundle = new WWW(path);  
  48.            
  49.          yield return bundle;  
  50.            
  51.          //加载到游戏中  
  52.          yield return Instantiate(bundle.assetBundle.mainAsset);  
  53.            
  54.          bundle.assetBundle.Unload(false);  
  55.     }  
  56.       
  57.     //读取全部资源  
  58.       
  59.     private IEnumerator LoadALLGameObject(string path)  
  60.     {  
  61.          WWW bundle = new WWW(path);  
  62.            
  63.          yield return bundle;  
  64.            
  65.          //通过Prefab的名称把他们都读取出来  
  66.          Object  obj0 =  bundle.assetBundle.Load("Prefab0");  
  67.          Object  obj1 =  bundle.assetBundle.Load("Prefab1");  
  68.           
  69.          //加载到游戏中     
  70.          yield return Instantiate(obj0);  
  71.          yield return Instantiate(obj1);  
  72.          bundle.assetBundle.Unload(false);  
  73.     }  
  74.       
  75.     private IEnumerator LoadMainCacheGameObject(string path)  
  76.     {  
  77.          WWW bundle = WWW.LoadFromCacheOrDownload(path,5);  
  78.            
  79.          yield return bundle;  
  80.            
  81.          //加载到游戏中  
  82.          yield return Instantiate(bundle.assetBundle.mainAsset);  
  83.            
  84.          bundle.assetBundle.Unload(false);  
  85.     }  
  86.       
  87.       
  88.     private IEnumerator LoadScene()  
  89.     {  
  90.          WWW download = WWW.LoadFromCacheOrDownload ("file://"+Application.dataPath + "/MyScene.unity3d", 1);  
  91.               
  92.           yield return download;  
  93.           var bundle = download.assetBundle;  
  94.           Application.LoadLevel ("Level");  
  95.     }  
  96. }  


如果assetbundle文件放在服务器端,直接用www.loadfromcacheordownload()通过版本来控制是否从服务器下载并保存到本地。本人亲自测试,这个方法是能下载到本地的,至于下载在本地哪儿我就不太清楚了,希望知道的同志们告知一下!


0 0
原创粉丝点击