[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

相关内容

热门资讯

失控的监护“外包”:孩子被送特... 从封闭式特训机构出来三个月后,李雨有时仍感觉自己还在里面。她忍不住看相关新闻。她看到报道中,一名21...
一举创下3项世界纪录,中国自主... 【环球时报报道 记者 郭媛丹】21日上午,中船集团旗下中船澄西船舶修造有限公司为力拓集团建造的自主研...
试图把国内矛盾外引?专家解析菲... 【环球时报报道 记者 樊巍】21日上午,外交部边海司负责人就菲律宾在仁爱礁蓄意挑衅、袭击中国海警执法...
特朗普宣布两年后对仿制药征收1... △美国总统特朗普(资料图)当地时间7月21日,美国总统特朗普在社交媒体发文表示,自2026年8月1日...
美国贸易代表暗示将很快出台新关... 新华社纽约7月21日电(记者刘亚南) 美国贸易代表贾米森·格里尔21日在接受媒体采访时暗示,美国联邦...
中国空调“一机难求”,人民日报... 中国空调“一机难求”,创意产品持续热销,本报记者实地探访——消暑降温,欧洲民众为何追捧中国制造?(第...
美防长:对伊朗战争已耗资375... △美国国防部长赫格塞思(资料图)央视记者当地时间21日获悉,美国国防部长赫格塞思表示,美国对伊朗战争...
在全国抗议声中,泽连斯基解除乌... 据乌克兰媒体《基辅独立报》报道,乌克兰总统泽连斯基解除了乌克兰武装部队总司令瑟尔斯基的职务,并任命联...
也门胡塞武装:停靠沙特港口船只... 新华社科威特城7月21日电(记者尹炣)萨那消息:也门胡塞武装20日警告国际航运公司称,与沙特港口开展...
特朗普:胡塞武装若封锁红海,美... 新华社华盛顿7月21日电(记者黄强 徐剑梅)美国总统特朗普21日在白宫会见到访的黎巴嫩总统奥恩时表示...