ResourcesComponent.cs 3.4 KB

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