打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【新提醒】【【知识摘录】 Unity3D AssetBundle资源加载-IOS (二)】
本帖最后由 海莉zhe 于 2017-1-11 10:35 编辑

    今天小编继续之前的AssetBundle的分享,在IOS上如何打包Assetbundle和加载Assetbundle资源,我们知道有很多类似的文章,但是还是想简单和大家一起学习一下,希望对一些刚刚学习unity3d的同学有帮助。
   好吧!废话不多讲,我直接上图上操作哈!


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

   2.第一步,我们先创建打包AssetBundle 的环境的,项目需要一个内置的文件夹Editor (必须的,这是为了可以在项目编辑器中打包AssetBundle资源),并且把AssetBundleEditor.cs组件类放在该文件下面。 [ 需要注意的是 :不同的平台参数是不一样的,IOS平台上Assetbundle打包时需要使用 BuildTarget.iPhone 参数。]
  


  

    AssetBundleEditor.cs 代码如下:
   
[C#] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
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
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 代码如下:
   
[C#] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
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
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();

  学习交流群:575561285
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Unity3D研究院之Assetbundle的实战(六十三)
[unity3d]保存文件到本地and加载本地文件
Unity中资源打包成Assetsbundle的资料整理
Unity3D 热更新方案(集合各位专家的汇总)
Unity C# 打包AssetBundle与场景
unity 加载资源
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服