【知识摘录】 Unity3D AssetBundle资源加载-IOS (二)

发表于2017-01-15
评论0 4.6k浏览
  今天小编继续之前的AssetBundle的分享,在IOS上如何打包Assetbundle和加载Assetbundle资源,我们知道有很多类似的文章,但是还是想简单和大家一起学习一下,希望对一些刚刚学习unity3d的同学有帮助。
  好吧!废话不多讲,我直接上图上操作哈!
1、首先还是新建一个unity3d项目,导入需要资源文件,我这边导入一个asset store 模型插件unity-chan资源。
2、第一步,我们先创建打包AssetBundle 的环境的,项目需要一个内置的文件夹Editor (必须的,这是为了可以在项目编辑器中打包AssetBundle资源),并且把AssetBundleEditor.cs组件类放在该文件下面。 [ 需要注意的是 :不同的平台参数是不一样的,IOS平台上Assetbundle打包时需要使用 BuildTarget.iPhone 参数。]



  AssetBundleEditor.cs 代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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配置好以后,记得重新AssetBundle打包模型。




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();
腾讯GAD游戏程序交流群:484290331Gad游戏开发核心用户群

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引