BundleRepository.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace XGame.Framework.Asset
  4. {
  5. //internal partial class AssetBundleLoader
  6. //{
  7. //}
  8. /// <summary>
  9. /// AssetBundleLoader已加载的Bundle仓库
  10. /// </summary>
  11. internal class BundleRepository
  12. {
  13. /// <summary>
  14. /// 已加载的Bundle
  15. /// key:bundleName
  16. /// </summary>
  17. private Dictionary<string, BundleAsset> BundleMap { set; get; }
  18. /// <summary>
  19. /// 已加载的Asset的InstanceId
  20. /// UnityEngine.Object用InstanceId代替HashCode,hash值可能出现重复
  21. /// </summary>
  22. private Dictionary<int, string> UsingHashCodeMap { set; get; }
  23. /// <summary>
  24. /// 等待卸载的Asset
  25. /// </summary>
  26. private Dictionary<string, Object> UnusedAssetMap { set; get; }
  27. public BundleRepository()
  28. {
  29. BundleMap = new Dictionary<string, BundleAsset>();
  30. UsingHashCodeMap = new Dictionary<int, string>();
  31. UnusedAssetMap = new Dictionary<string, Object>();
  32. }
  33. #region Bundle
  34. public bool ContainsBundle(string bundleName)
  35. {
  36. return BundleMap.ContainsKey(bundleName);
  37. }
  38. public void AddBundle(BundleAsset bundleAsset)
  39. {
  40. if (bundleAsset == null)
  41. {
  42. AssetsLog.Warn("Bundle Repository Add Invalid Bundle.");
  43. return;
  44. }
  45. var bundleName = bundleAsset.AddressableName;
  46. if (BundleMap.ContainsKey(bundleName))
  47. {
  48. AssetsLog.Warn($"Add Bundle Repeat. Name:{bundleName}");
  49. return;
  50. }
  51. BundleMap.Add(bundleName, bundleAsset);
  52. }
  53. public BundleAsset GetBundle(string bundleName)
  54. {
  55. BundleMap.TryGetValue(bundleName, out var bundle);
  56. return bundle;
  57. }
  58. public void RemoveBundle(string bundleName)
  59. {
  60. BundleMap.Remove(bundleName);
  61. }
  62. public void RetainBundle(string bundleName)
  63. {
  64. if (BundleMap.TryGetValue(bundleName, out var bundle))
  65. {
  66. bundle.Retain();
  67. }
  68. }
  69. public void ReleaseBundle(string bundleName)
  70. {
  71. if (BundleMap.TryGetValue(bundleName, out var bundle))
  72. {
  73. bundle.Release();
  74. }
  75. }
  76. #endregion
  77. #region AssetHashCode
  78. public bool RegistAssetHash(int hashCode, string addressableName)
  79. {
  80. if (!UsingHashCodeMap.ContainsKey(hashCode))
  81. {
  82. UsingHashCodeMap.Add(hashCode, addressableName);
  83. return true;
  84. }
  85. AssetsLog.Warn($"Bundle asset hash repeat. Hash:{hashCode} Last:{UsingHashCodeMap[hashCode]} Next:{addressableName}");
  86. return false;
  87. }
  88. // ReSharper disable once IdentifierTypo
  89. public void UnRegistAssetHash(int hashCode)
  90. {
  91. if (UsingHashCodeMap.ContainsKey(hashCode))
  92. {
  93. UsingHashCodeMap.Remove(hashCode);
  94. }
  95. }
  96. public string GetAssetNameByHash(int hashCode)
  97. {
  98. UsingHashCodeMap.TryGetValue(hashCode, out var addressableName);
  99. return addressableName;
  100. }
  101. public bool ContainsAssetHash(int hashCode)
  102. {
  103. return UsingHashCodeMap.ContainsKey(hashCode);
  104. }
  105. public bool ContainsAsset(string addressableName)
  106. {
  107. return UsingHashCodeMap.ContainsValue(addressableName);
  108. }
  109. #endregion
  110. #region UnusedAsset
  111. public bool ContainsUnusedAsset(string addressableName)
  112. {
  113. UnusedAssetMap.TryGetValue(addressableName, out var asset);
  114. return asset != null;
  115. }
  116. public void PushUnusedAsset(Object source)
  117. {
  118. if (source == null) return;
  119. var instanceId = source.GetInstanceID();
  120. var addressableName = GetAssetNameByHash(instanceId);
  121. if (string.IsNullOrEmpty(addressableName))
  122. {
  123. AssetsLog.Debug($"Object is unregistered. Hash:{instanceId} Name:{source.name}");
  124. return;
  125. }
  126. if (UnusedAssetMap.ContainsKey(addressableName))
  127. {
  128. AssetsLog.Debug($"Object had in map. But add repeat. Name:{addressableName} Next:{instanceId} Last:{UnusedAssetMap[addressableName]?.GetInstanceID()}");
  129. return;
  130. }
  131. UnusedAssetMap.Add(addressableName, source);
  132. }
  133. public Object PopUnusedAsset(string addressableName)
  134. {
  135. if (UnusedAssetMap.TryGetValue(addressableName, out var source))
  136. {
  137. UnusedAssetMap.Remove(addressableName);
  138. }
  139. return source;
  140. }
  141. public void UnloadUnusedAssets(System.Func<string, bool> isReferencing, System.Action<string> onUnloadAsset)
  142. {
  143. //释放未被引用的Asset
  144. var unloadList = new List<string>();
  145. foreach (var item in UnusedAssetMap)
  146. {
  147. var addressableName = item.Key;
  148. if (!isReferencing(item.Key))
  149. {
  150. unloadList.Add(addressableName);
  151. }
  152. }
  153. foreach (var addressableName in unloadList)
  154. {
  155. var asset = UnusedAssetMap[addressableName];
  156. var instanceId = asset?.GetInstanceID() ?? 0;
  157. UnRegistAssetHash(instanceId);
  158. UnusedAssetMap.Remove(addressableName);
  159. onUnloadAsset?.Invoke(addressableName);
  160. asset.Unload();
  161. //if (asset is Component || asset is GameObject || asset is AssetBundle)
  162. //{
  163. // //非独立资源
  164. // //Object.Destroy(asset);
  165. //}
  166. //else
  167. //{
  168. // //独立资源才能使用Resources.UnloadAsset
  169. // Resources.UnloadAsset(asset);
  170. //}
  171. AssetsLog.Debug($"Resources UnloadAsset: {addressableName}");
  172. }
  173. unloadList.Clear();
  174. //释放引用计数为零的Bundle
  175. foreach (var bundleItem in BundleMap)
  176. {
  177. if (bundleItem.Value.Reference <= 0)
  178. {
  179. unloadList.Add(bundleItem.Key);
  180. }
  181. }
  182. foreach (var bundlePath in unloadList)
  183. {
  184. var bundleAsset = BundleMap[bundlePath];
  185. BundleMap.Remove(bundlePath);
  186. bundleAsset.Dispose();
  187. AssetsLog.Debug($"AssetBundle Unload: {bundlePath}");
  188. }
  189. unloadList.Clear();
  190. Resources.UnloadUnusedAssets();
  191. }
  192. #endregion
  193. public void Dispose()
  194. {
  195. foreach (var asset in UnusedAssetMap)
  196. {
  197. asset.Value.Unload();
  198. //Resources.UnloadAsset(asset.Value);
  199. }
  200. UnusedAssetMap.Clear();
  201. foreach (var it in BundleMap)
  202. {
  203. it.Value.Dispose();
  204. }
  205. BundleMap.Clear();
  206. UsingHashCodeMap.Clear();
  207. }
  208. }
  209. }