今天分享下,在IOS上如何打包Assetbundle和加载Assetbundle,我们知道有很多类似的文章,但是还是想简单和大家一起学习一下,希望对一些刚刚学习unity3d的同学有帮助。


   好吧!废话不多讲,直接上图上操作哈!


  1.首先还是新建一个unity3d项目,导入需要资源文件,我这边导入一个asset store 模型插件 unity-chan资源。


AssetBundle


 



 




  AssetBundleEditor.cs




using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
 
public class AssetBundleEditor : Editor 
{
 
        public static string sourcePath = Application.dataPath + "/Resource";
        private static string AssetBundlesOutputPath = Application.dataPath + "/StreamingAssets";
 
        [MenuItem("AssetBundles/BuildAssetBundle")]
        public static void BuildAssetBundle()
        {
                string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(BuildTarget.iOS));
                if (!Directory.Exists(outputPath))
                {
                        Directory.CreateDirectory(outputPath);
                }
 
                //根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包
                BuildPipeline.BuildAssetBundles("Assets/StreamingAssets/IOS",BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS);
 
                AssetDatabase.Refresh();
 
                Debug.Log("打包完成");
 
        }
}
 
public class Platform
{
        public static string GetPlatformFolder(BuildTarget target)
        {
                switch (target)
                {
                case BuildTarget.Android:
                        return "Android";
                case BuildTarget.iOS:
                        return "IOS";
                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneWindows64:
                        return "Windows";
                case BuildTarget.StandaloneOSXIntel:
                case BuildTarget.StandaloneOSXIntel64:
                case BuildTarget.StandaloneOSXUniversal:
                        return "OSX";
                default:
                        return null;
                }
        }
}







  3.第二步,我们把打包好的AssetBundle资源解包出来,现在在场景中新建一个LoadAssetBundle的对象,实现加载asset bundle资源的脚本。


LoadAssetBundle.cs 代码如下: 




using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class LoadAssetBundle : MonoBehaviour
{
 
        public  string PathURL;
        public GameObject CurrentObj;
 
        void Start()
        {
                #if UNITY_IPHONE  
                PathURL = "file://"+Application.dataPath + "/Raw/IOS";  
                #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  
                PathURL = "file://"+Application.streamingAssetsPath+"/IOS";
                #endif  
 
        }
 
        void OnGUI()
        {
                if(GUI.Button(new Rect(100,100,100,100),"加载美少女"))
                {
                        StartCoroutine (GetAssetBundleObj("girl_001",PathURL));
                }
        }
 
        public  IEnumerator GetAssetBundleObj(string objName,string path="")
        {
                string filePath = System.IO.Path.Combine (path,objName);
 
                WWW w = new WWW(filePath);         //利用www类加载
                yield return w;
                AssetBundle curBundleObj = w.assetBundle;     //获得AssetBundle
 
                AssetBundleRequest obj = curBundleObj.LoadAssetAsync(objName, typeof(GameObject));    //异步加载GameObject类型
                yield return obj;
                CurrentObj = Instantiate(obj.asset) as GameObject;
 
                yield return null;
                curBundleObj.Unload(false);     //卸载所有包含在bundle中的对象,已经加载的才会卸载
                w.Dispose();
        }
 
}





   


  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(); 



Unity教程之-Unity3d打包Assetbundle并加载(全面)