ResourcesComponent.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. namespace Model
  6. {
  7. public class ResourcesComponent: Component
  8. {
  9. public static AssetBundleManifest AssetBundleManifestObject { get; set; }
  10. private readonly Dictionary<string, UnityEngine.Object> resourceCache = new Dictionary<string, UnityEngine.Object>();
  11. private readonly Dictionary<string, AssetBundle> bundleCaches = new Dictionary<string, AssetBundle>();
  12. public K GetReference<K>(string bundle, string prefab, string key) where K : class
  13. {
  14. GameObject gameObject = this.GetAsset<GameObject>(bundle, prefab);
  15. return gameObject.GetComponent<ReferenceCollector>().Get<K>(key);
  16. }
  17. public K GetAsset<K>(string bundleName, string prefab) where K : class
  18. {
  19. string path = $"{bundleName}.unity3d/{prefab}".ToLower();
  20. UnityEngine.Object resource = null;
  21. if (this.resourceCache.TryGetValue(path, out resource))
  22. {
  23. return resource as K;
  24. }
  25. if (Define.IsAsync)
  26. {
  27. if (!this.bundleCaches.ContainsKey($"{bundleName}.unity3d".ToLower()))
  28. {
  29. return null;
  30. }
  31. throw new Exception($"异步加载资源,资源不在resourceCache中: {bundleName} {path}");
  32. }
  33. try
  34. {
  35. resource = ResourceHelper.LoadResource(bundleName, prefab);
  36. this.resourceCache.Add(path, resource);
  37. }
  38. catch (Exception e)
  39. {
  40. throw new Exception($"加载资源出错,输入路径:{path}", e);
  41. }
  42. return resource as K;
  43. }
  44. public async Task DownloadAndCacheAsync(string uri, string assetBundleName)
  45. {
  46. assetBundleName = (assetBundleName + ".unity3d").ToLower();
  47. AssetBundle assetBundle;
  48. // 异步下载资源
  49. string url = uri + "StreamingAssets/" + assetBundleName;
  50. int count = 0;
  51. TimerComponent timerComponent = Game.Scene.GetComponent<TimerComponent>();
  52. while (true)
  53. {
  54. using (WWWAsync wwwAsync = new WWWAsync())
  55. {
  56. try
  57. {
  58. ++count;
  59. if (count > 1)
  60. {
  61. await timerComponent.WaitAsync(1000);
  62. }
  63. if (this.Id == 0)
  64. {
  65. return;
  66. }
  67. await wwwAsync.LoadFromCacheOrDownload(url, ResourcesComponent.AssetBundleManifestObject.GetAssetBundleHash(assetBundleName));
  68. assetBundle = wwwAsync.www.assetBundle;
  69. break;
  70. }
  71. catch (Exception e)
  72. {
  73. Log.Error(e.ToString());
  74. }
  75. }
  76. }
  77. if (!assetBundle.isStreamedSceneAssetBundle)
  78. {
  79. // 异步load资源到内存cache住
  80. UnityEngine.Object[] assets;
  81. using (AssetBundleLoaderAsync assetBundleLoaderAsync = new AssetBundleLoaderAsync(assetBundle))
  82. {
  83. assets = await assetBundleLoaderAsync.LoadAllAssetsAsync();
  84. }
  85. foreach (UnityEngine.Object asset in assets)
  86. {
  87. string path = $"{assetBundleName}/{asset.name}".ToLower();
  88. this.resourceCache[path] = asset;
  89. }
  90. }
  91. if (this.bundleCaches.ContainsKey(assetBundleName))
  92. {
  93. throw new Exception($"重复加载资源: {assetBundleName}");
  94. }
  95. this.bundleCaches[assetBundleName] = assetBundle;
  96. }
  97. public override void Dispose()
  98. {
  99. if (this.Id == 0)
  100. {
  101. return;
  102. }
  103. base.Dispose();
  104. foreach (var assetBundle in bundleCaches)
  105. {
  106. assetBundle.Value?.Unload(true);
  107. }
  108. }
  109. }
  110. }