ResourcesComponent.cs 3.3 KB

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