查看: 1399|回复: 2
打印 上一主题 下一主题

[其他] Unity3D技术之跟踪已下载资源包浅析

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-1-31 18:32:56 |只看该作者 |倒序浏览
Unity 一次只允许加载一个特定资源包 (AssetBundle) 实例到应用程序中。这意味着您无法检索 WWW 对象中之前已加载的相同资源包 (AssetBundle) 或尚未加载的资源包。实际上,这意味着您尝试访问之前已下载的如下资源包 (AssetBundle) 时:

AssetBundle bundle = www.assetBundle;

程序将引出以下错误

Cannot load cached AssetBundle.A file of the same name is already loaded from another AssetBundle

且资源包属性将返回 null。如果第一次下载时下载了该资源包 (AssetBundle),则第二次下载时无法检索到该资源包,因此,无需再使用该资源包时,可以将其卸载 或获取其引用,并在它存在于内存中时避免将其下载。可根据需求决定需要采取的相应措施,但我们建议在加载完对象后立即卸载该资源包 (AssetBundle)。这可释放内存空间,并且不会再收到有关加载已缓存资源包 (AssetBundles) 的错误。

如需跟踪已下载资源包 (AssetBundles),可使用包装类协助管理下载,如下:
  1. <pre class="">using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;

  5. static public class AssetBundleManager {
  6. // A dictionary to hold the AssetBundle references
  7. static private Dictionary<string, AssetBundleRef> dictAssetBundleRefs;
  8. static AssetBundleManager (){
  9. dictAssetBundleRefs = new Dictionary<string, AssetBundleRef>();
  10.    }
  11. // Class with the AssetBundle reference, url and version
  12. private class AssetBundleRef {
  13. public AssetBundle assetBundle = null;
  14. public int version;
  15. public string url;
  16. public AssetBundleRef(string strUrlIn, int intVersionIn) {
  17. url = strUrlIn;
  18. version = intVersionIn;
  19.        }
  20.    };
  21. // Get an AssetBundle
  22. public static AssetBundle getAssetBundle (string url, int version){
  23. string keyName = url + version.ToString();
  24. AssetBundleRef abRef;
  25. if (dictAssetBundleRefs.TryGetValue(keyName, out abRef))
  26. return abRef.assetBundle;
  27. else
  28. return null;
  29.    }
  30. // Download an AssetBundle
  31. public static IEnumerator downloadAssetBundle (string url, int version){
  32. string keyName = url + version.ToString();
  33. if (dictAssetBundleRefs.ContainsKey(keyName))
  34. yield return null;
  35. else {
  36. using(WWW www = WWW.LoadFromCacheOrDownload (url, version)){
  37. yield return www;
  38. if (www.error != null)
  39. throw new Exception("WWW download:"+ www.error);
  40. AssetBundleRef abRef = new AssetBundleRef (url, version);
  41. abRef.assetBundle = www.assetBundle;
  42. dictAssetBundleRefs.Add (keyName, abRef);
  43.            }
  44.        }
  45.    }
  46. // Unload an AssetBundle
  47. public static void Unload (string url, int version, bool allObjects){
  48. string keyName = url + version.ToString();
  49. AssetBundleRef abRef;
  50. if (dictAssetBundleRefs.TryGetValue(keyName, out abRef)){
  51. abRef.assetBundle.Unload (allObjects);
  52. abRef.assetBundle = null;
  53. dictAssetBundleRefs.Remove(keyName);
  54.        }
  55.    }
  56. }</pre><p style="margin-bottom: 15px;">An example usage of the class would be:</p><pre>using UnityEditor;

  57. class ManagedAssetBundleExample :MonoBehaviour {
  58. public string url;
  59. public int version;
  60. AssetBundle bundle;
  61. void OnGUI (){
  62. if (GUILayout.Label ("Download bundle"){
  63. bundle = AssetBundleManager.getAssetBundle (url, version);
  64. if(!bundle)
  65. StartCoroutine (DownloadAB());
  66.        }
  67.    }
  68. IEnumerator DownloadAB (){
  69. yield return StartCoroutine(AssetBundleManager.downloadAssetBundle (url, version));
  70. bundle = AssetBundleManager.getAssetBundle (url, version);
  71.    }
  72. void OnDisable (){
  73. AssetBundleManager.Unload (url, version);
  74.    }
  75. }</pre>
复制代码
请记住,本示例中的 AssetBundleManager 类是静态的,正在引用的任何资源包 (AssetBundles) 不会在加载新场景时销毁。请将此类当做指南例程,但正如最初建议的那样,最好在使用后立即卸载资源包 (AssetBundles)。您始终可以克隆之前实例化的对象,无需再下载该资源包 (AssetBundles)。

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

3

主题

1

听众

6189

积分

高级设计师

Rank: 6Rank: 6

纳金币
370
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2015-1-31 18:48:31 |只看该作者
Thanks for sharing!
回复

使用道具 举报

100

主题

3

听众

7683

积分

高级设计师

Rank: 6Rank: 6

纳金币
2378
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2015-1-31 20:51:58 |只看该作者
感谢分享。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-15 06:49 , Processed in 0.084235 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部