[Unity3D]AssetBundles的使用
admin
2023-01-20 20:22:48
0

 一共有两种方法下载AssetBundles数据资源:

  1. 无缓存:这种方法使用将创建一个WWW类,下载完的数据无法在本地unity3d的缓存目录中进行保存。

  2. 有缓存:使用WWW.LoadFromCacheOrDownload的方法,下载完的数据将在unity3d的本地缓存目录中进行保存。Web浏览器通常允许缓存大小达到50MB,PC和MAC的本地应用,IOS和Android应用都允许缓存达到4GB大小。

   下面是不含缓存的代码:

using System;

using UnityEngine;

using System.Collections; class NonCachingLoadExample : MonoBehaviour {

  public string BundleURL;

  public string AssetName;

  IEnumerator Start() {

  // Download the file from the URL. It will not be saved in the Cache

  using (WWW www = new WWW(BundleURL)) {

  yield return www;

  if (www.error != null)

  throw new Exception("WWW download had an error:" + www.error);

  AssetBundle bundle = www.assetBundle;

  if (AssetName == "")

  Instantiate(bundle.mainAsset);

  else

  Instantiate(bundle.Load(AssetName));

                  // Unload the AssetBundles compressed contents to conserve memory

                  bundle.Unload(false);

  }

  }

}

   下面是含缓存的代码,系统建议利用WWW.LoadFromCacheOrDownload类进行下载:

using System;

using UnityEngine;

using System.Collections;

 

public class CachingLoadExample : MonoBehaviour {

public string BundleURL;

public string AssetName;

public int version;

 

void Start() {

StartCoroutine (DownloadAndCache());

}

 

IEnumerator DownloadAndCache (){

// Wait for the Caching system to be ready

while (!Caching.ready)

yield return null;

 

// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache

using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){

yield return www;

if (www.error != null)

throw new Exception("WWW download had an error:" + www.error);

AssetBundle bundle = www.assetBundle;

if (AssetName == "")

Instantiate(bundle.mainAsset);

else

Instantiate(bundle.Load(AssetName));

               // Unload the AssetBundles compressed contents to conserve memory

               bundle.Unload(false);

}

}

}

   这样,下载之前系统会现在缓存目录中查找, WWW.LoadFromCacheOrDownload函数的第二个参数表示版本号,当要下载的数据在缓存目录中不存在,或者存在,但版本号比较低时,系统才会下载新的数据资源,并替换到缓存中的原数据资源。

   现在来完成上一篇中的实例,在项目视图(Projection View)中,建立Script目录,利用上面的两段源代码,分别创建名为CachingLoadExample和NonCachingLoadExample的C#脚本,如下图所示:

[Unity3D]AssetBundles的使用

   在unity3d编辑器中创建空物体,菜单GameObject - CreateEmpty,把CachingLoadExample脚本拖动到GameObject上。在层级视图中(Hierarchy View)选中GameObject,在右边的监视视图(Inspector View)中,把CachingLoadExample脚本的BundleURL变量值指定到Cube.unity3d文件所在位置(输入绝对路径,需根据自己存放的目录手动修改),例如:file://C:/UnityProjects/AssetBundlesGuide/Assets/AssetBundles/Cube.unity3d,现在运行unity3d,就能够实现动态加载Cube物体了。

   实例文件下载地址:http://download.csdn.net/detail/s10141303/6496017

相关内容

热门资讯

玩家分享攻略“一言棋牌.有没有... 有 亲,根据资深记者爆料一言棋牌是可以开挂的,确实有挂(咨询软件无需打开...
【今日要闻】“欢记联盟.辅助器... 有 亲,根据资深记者爆料欢记联盟是可以开挂的,确实有挂(咨询软件无需打开...
今日重大发现“杭州麻将.怎么开... 网上科普关于“杭州麻将有没有挂”话题很是火热,小编也是针对杭州麻将作*弊开挂的方法以及开挂对应的知识...
【第一财经】“白金岛游戏.怎么... 您好:白金岛游戏这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游...
玩家最新攻略“奕福麻将.有没有... 家人们!今天小编来为大家解答奕福麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...
【今日要闻】“麻友圈2贵阳捉鸡... 有 亲,根据资深记者爆料麻友圈2贵阳捉鸡是可以开挂的,确实有挂(咨询软件...
今日重大发现“聚友互娱.怎么开... 有 亲,根据资深记者爆料聚友互娱是可以开挂的,确实有挂(咨询软件无需打开...
重磅消息“炫龙牛牛.到底有挂吗... 您好:炫龙牛牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
今日重磅消息“皇豪众娱牛牛.辅... 有 亲,根据资深记者爆料皇豪众娱牛牛是可以开挂的,确实有挂(咨询软件无需...
终于明白“经典联盟牛牛.开挂器... 家人们!今天小编来为大家解答经典联盟牛牛透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...