近期热门
粉丝4
关注 0
获赞 2
Unity3D AssetBundle 资源加载-IOS

[U3D] Unity3D AssetBundle 资源加载-IOS

[复制链接]
1705 0 0 0 7年前 举报
今天继续之前的AssetBundle的分享,在IOS上如何打包Assetbundle和加载Assetbundle,我们知道有很多类似的文章,但是还是想简单和大家一起学习一下,希望对一些刚刚学习unity3d的同学有帮助。欢迎加我的Unity学习交流群:575561285。
    好吧!废话不多讲,我直接上图上操作哈!
  1.首先还是新建一个unity3d项目,导入需要资源文件,我这边导入一个asset store 模型插件unity-chan资源。
  2.第一步,我们先创建打包AssetBundle 的环境的,项目需要一个内置的文件夹Editor (这是必须操作,这是为了可以在项目编辑器中打包AssetBundle资源),并且附上AssetBundleEditor.cs组件类。  需要注意的是 :不同的平台参数是不一样的,IOS平台上Assetbundle打包时需要使用 BuildTarget.iPhone 参数。
1.png

3.png

  AssetBundleEditor.cs 代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.IO;

  5. public class AssetBundleEditor : Editor
  6. {

  7.         public static string sourcePath = Application.dataPath + "/Resource";
  8.         private static string AssetBundlesOutputPath = Application.dataPath + "/StreamingAssets";

  9.         [MenuItem("AssetBundles/BuildAssetBundle")]
  10.         public static void BuildAssetBundle()
  11.         {
  12.                 string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(BuildTarget.iOS));
  13.                 if (!Directory.Exists(outputPath))
  14.                 {
  15.                         Directory.CreateDirectory(outputPath);
  16.                 }

  17.                 //根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包
  18.                 BuildPipeline.BuildAssetBundles("Assets/StreamingAssets/IOS",BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS);

  19.                 AssetDatabase.Refresh();

  20.                 Debug.Log("打包完成");

  21.         }
  22. }

  23. public class Platform
  24. {
  25.         public static string GetPlatformFolder(BuildTarget target)
  26.         {
  27.                 switch (target)
  28.                 {
  29.                 case BuildTarget.Android:
  30.                         return "Android";
  31.                 case BuildTarget.iOS:
  32.                         return "IOS";
  33.                 case BuildTarget.StandaloneWindows:
  34.                 case BuildTarget.StandaloneWindows64:
  35.                         return "Windows";
  36.                 case BuildTarget.StandaloneOSXIntel:
  37.                 case BuildTarget.StandaloneOSXIntel64:
  38.                 case BuildTarget.StandaloneOSXUniversal:
  39.                         return "OSX";
  40.                 default:
  41.                         return null;
  42.                 }
  43.         }
  44. }
复制代码
  3.第二步,我们把打包好的AssetBundle资源解包出来,现在在场景中新建一个LoadAssetBundle的对象,实现加载asset bundle资源的脚本。
LoadAssetBundle.cs 代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;

  4. public class LoadAssetBundle : MonoBehaviour
  5. {

  6.         public  string PathURL;
  7.         public GameObject CurrentObj;

  8.         void Start()
  9.         {
  10.                 #if UNITY_IPHONE  
  11.                 PathURL = "file://"+Application.dataPath + "/Raw/IOS";  
  12.                 #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  
  13.                 PathURL = "file://"+Application.streamingAssetsPath+"/IOS";
  14.                 #endif  

  15.         }

  16.         void OnGUI()
  17.         {
  18.                 if(GUI.Button(new Rect(100,100,100,100),"加载美少女"))
  19.                 {
  20.                         StartCoroutine (GetAssetBundleObj("girl_001",PathURL));
  21.                 }
  22.         }

  23.         public  IEnumerator GetAssetBundleObj(string objName,string path="")
  24.         {
  25.                 string filePath = System.IO.Path.Combine (path,objName);

  26.                 WWW w = new WWW(filePath);         //利用www类加载
  27.                 yield return w;
  28.                 AssetBundle curBundleObj = w.assetBundle;     //获得AssetBundle

  29.                 AssetBundleRequest obj = curBundleObj.LoadAssetAsync(objName, typeof(GameObject));    //异步加载GameObject类型
  30.                 yield return obj;
  31.                 CurrentObj = Instantiate(obj.asset) as GameObject;

  32.                 yield return null;
  33.                 curBundleObj.Unload(false);     //卸载所有包含在bundle中的对象,已经加载的才会卸载
  34.                 w.Dispose();
  35.         }

  36. }
复制代码
   
  4.其实从运行项目的时候,我们会发现模型的加载不正常,因为它丢失了材质,而我们找到的解决的方法是在Edit->Project Setting->Graphics 设置中的Always Include Shaders 中增加模型的Shader,Shader配置好以后,记得重新打包模型。
4_0.png
4_1.png
4_2.png

5.好吧,接下来我们Build一个版本在真机上,这个涉及到 IOS 开发部署流程,有兴趣的同学可以去研究一下,然后,我在这边直接操作啦!
  5.0 首先BuildSetting 切换平台到IOS;
5_0.png
   
   5.1 直接在真机上的效果。
5_1.png
  
  
最后,我们来看一下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();


0
点赞
0
打赏
0
添加到收藏夹

0

点击复制链接

使用微信扫码分享
一次扣10个券
全部评论0
您需要登录后才可以回帖 登录 | 立即注册

暂无评论,去成为第一人吧