[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

相关内容

热门资讯

今年上半年全国城镇新增就业69... 今天(22日)上午,人力资源社会保障部举行新闻发布会。今年上半年,全国城镇新增就业695万人,同比持...
委内瑞拉强震遇难人数升至534... 当地时间21日,委内瑞拉全国代表大会主席豪尔赫·罗德里格斯通过社交媒体通报,该国6月24日两次强震造...
高考684分花3000元咨询进... 潮新闻客户端 青年特约评论员 曾耀湘图源:视觉中国近日,据齐鲁晚报报道,河北一名2018级考生,因“...
郑州2死2伤火灾系人为纵火 郑州市公安局金水分局对外发布案件通报。通报显示,6月19日21时,郑州市金水区宏明路与柳林路东南角宏...
29岁成都小伙旅行中改道广西救... 7月14日,自发赶往广西救灾的志愿者党鑫蕊,由于劳累过度导致心功能衰竭,在贵港市覃塘区不幸离世,年仅...
清理洗衣机下水管道的方法 清理洗衣机下水管道是一项必要的任务,如果不及时清理,管道内部容易积累污垢和细菌,从而影响洗衣机的使用...
澳外长宣称:中国要就军事建设做... 7月初,中国向太平洋公海海域成功发射一枚携载训练模拟弹头的潜射战略导弹。中方重申,此为例行军事训练活...
伊朗威胁打击海水淡化厂,到底有... 据路透社报道,伊朗7月21日对科威特一座海水淡化厂发动袭击,引发海湾地区担忧,随着该地区冲突升级,更...
美新版“空军一号”刚上岗就回炉... 【环球时报综合报道】美国《防务新闻》7月20日称,美国总统特朗普透露,新版总统专机“空军一号”将被送...
全自动卧式洗衣机清理方法 随着生活水平的不断提高,全自动卧式洗衣机已经成为了家庭生活中不可或缺的一部分。它的出现方便了我们日常...