ResourcesComponent.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace Model
  11. {
  12. public class ABInfo: Disposer
  13. {
  14. private int refCount;
  15. public string Name { get; }
  16. public int RefCount
  17. {
  18. get
  19. {
  20. return this.refCount;
  21. }
  22. set
  23. {
  24. Log.Debug($"{this.Name} refcount: {value}");
  25. this.refCount = value;
  26. }
  27. }
  28. public AssetBundle AssetBundle { get; }
  29. public ABInfo(string name, AssetBundle ab)
  30. {
  31. this.Name = name;
  32. this.AssetBundle = ab;
  33. this.RefCount = 1;
  34. Log.Debug($"load assetbundle: {this.Name}");
  35. }
  36. public override void Dispose()
  37. {
  38. if (this.Id == 0)
  39. {
  40. return;
  41. }
  42. base.Dispose();
  43. Log.Debug($"desdroy assetbundle: {this.Name}");
  44. this.AssetBundle?.Unload(true);
  45. }
  46. }
  47. public class ResourcesComponent : Component
  48. {
  49. public static AssetBundleManifest AssetBundleManifestObject { get; set; }
  50. private readonly Dictionary<string, UnityEngine.Object> resourceCache = new Dictionary<string, UnityEngine.Object>();
  51. private readonly Dictionary<string, ABInfo> bundles = new Dictionary<string, ABInfo>();
  52. // lru缓存队列
  53. private readonly QueueDictionary<string, ABInfo> cacheDictionary = new QueueDictionary<string, ABInfo>();
  54. public K GetAsset<K>(string bundleName, string prefab) where K : class
  55. {
  56. string path = $"{bundleName}.unity3d/{prefab}".ToLower();
  57. UnityEngine.Object resource = null;
  58. if (!this.resourceCache.TryGetValue(path, out resource))
  59. {
  60. throw new Exception($"not found asset: {path}");
  61. }
  62. return resource as K;
  63. }
  64. public void UnloadBundle(string assetBundleName)
  65. {
  66. assetBundleName = assetBundleName.ToLower();
  67. this.UnloadOneBundle(assetBundleName);
  68. string[] dependencies = ResourcesHelper.GetDependencies(assetBundleName);
  69. //Log.Debug($"-----------dep unload {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  70. foreach (string dependency in dependencies)
  71. {
  72. this.UnloadOneBundle(dependency);
  73. }
  74. }
  75. private void UnloadOneBundle(string assetBundleName)
  76. {
  77. assetBundleName = assetBundleName.ToLower();
  78. //Log.Debug($"unload bundle {assetBundleName}");
  79. ABInfo abInfo;
  80. if (!this.bundles.TryGetValue(assetBundleName, out abInfo))
  81. {
  82. throw new Exception($"not found assetBundle: {assetBundleName}");
  83. }
  84. --abInfo.RefCount;
  85. if (abInfo.RefCount > 0)
  86. {
  87. return;
  88. }
  89. this.bundles.Remove(assetBundleName);
  90. // 缓存10个包
  91. this.cacheDictionary.Enqueue(assetBundleName, abInfo);
  92. if (this.cacheDictionary.Count > 10)
  93. {
  94. abInfo = this.cacheDictionary[this.cacheDictionary.FirstKey];
  95. this.cacheDictionary.Dequeue();
  96. abInfo.Dispose();
  97. }
  98. Log.Debug($"cache count: {this.cacheDictionary.Count}");
  99. }
  100. /// <summary>
  101. /// 同步加载assetbundle
  102. /// </summary>
  103. /// <param name="assetBundleName"></param>
  104. /// <returns></returns>
  105. public void LoadBundle(string assetBundleName)
  106. {
  107. assetBundleName = assetBundleName.ToLower();
  108. this.LoadOneBundle(assetBundleName);
  109. string[] dependencies = ResourcesHelper.GetDependencies(assetBundleName);
  110. Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  111. foreach (string dependency in dependencies)
  112. {
  113. if (string.IsNullOrEmpty(dependency))
  114. {
  115. continue;
  116. }
  117. this.LoadOneBundle(dependency);
  118. }
  119. }
  120. public void LoadOneBundle(string assetBundleName)
  121. {
  122. ABInfo abInfo;
  123. if (this.bundles.TryGetValue(assetBundleName, out abInfo))
  124. {
  125. ++abInfo.RefCount;
  126. return;
  127. }
  128. if (this.cacheDictionary.ContainsKey(assetBundleName))
  129. {
  130. abInfo = this.cacheDictionary[assetBundleName];
  131. ++abInfo.RefCount;
  132. this.bundles[assetBundleName] = abInfo;
  133. this.cacheDictionary.Remove(assetBundleName);
  134. return;
  135. }
  136. if (!Define.IsAsync)
  137. {
  138. #if UNITY_EDITOR
  139. string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  140. foreach (string s in realPath)
  141. {
  142. string assetName = Path.GetFileNameWithoutExtension(s);
  143. string path = $"{assetBundleName}/{assetName}".ToLower();
  144. UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
  145. this.resourceCache[path] = resource;
  146. }
  147. this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
  148. return;
  149. #endif
  150. }
  151. AssetBundle assetBundle = AssetBundle.LoadFromFile(Path.Combine(PathHelper.AppResPath, assetBundleName));
  152. if (!assetBundle.isStreamedSceneAssetBundle)
  153. {
  154. // 异步load资源到内存cache住
  155. UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
  156. foreach (UnityEngine.Object asset in assets)
  157. {
  158. string path = $"{assetBundleName}/{asset.name}".ToLower();
  159. this.resourceCache[path] = asset;
  160. }
  161. }
  162. this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
  163. }
  164. /// <summary>
  165. /// 异步加载assetbundle
  166. /// </summary>
  167. /// <param name="assetBundleName"></param>
  168. /// <returns></returns>
  169. public async Task LoadBundleAsync(string assetBundleName)
  170. {
  171. assetBundleName = assetBundleName.ToLower();
  172. await this.LoadOneBundleAsync(assetBundleName);
  173. string[] dependencies = ResourcesHelper.GetDependencies(assetBundleName);
  174. //Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  175. foreach (string dependency in dependencies)
  176. {
  177. if (string.IsNullOrEmpty(dependency))
  178. {
  179. continue;
  180. }
  181. await this.LoadOneBundleAsync(dependency);
  182. }
  183. }
  184. public async Task LoadOneBundleAsync(string assetBundleName)
  185. {
  186. ABInfo abInfo;
  187. if (this.bundles.TryGetValue(assetBundleName, out abInfo))
  188. {
  189. ++abInfo.RefCount;
  190. return;
  191. }
  192. if (this.cacheDictionary.ContainsKey(assetBundleName))
  193. {
  194. abInfo = this.cacheDictionary[assetBundleName];
  195. ++abInfo.RefCount;
  196. this.bundles[assetBundleName] = abInfo;
  197. this.cacheDictionary.Remove(assetBundleName);
  198. return;
  199. }
  200. if (!Define.IsAsync)
  201. {
  202. #if UNITY_EDITOR
  203. string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  204. foreach (string s in realPath)
  205. {
  206. string assetName = Path.GetFileNameWithoutExtension(s);
  207. string path = $"{assetBundleName}/{assetName}".ToLower();
  208. UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
  209. this.resourceCache[path] = resource;
  210. }
  211. this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
  212. return;
  213. #endif
  214. }
  215. AssetBundle assetBundle;
  216. using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = EntityFactory.Create<AssetsBundleLoaderAsync>())
  217. {
  218. assetBundle = await assetsBundleLoaderAsync.LoadAsync(assetBundleName);
  219. }
  220. if (!assetBundle.isStreamedSceneAssetBundle)
  221. {
  222. // 异步load资源到内存cache住
  223. UnityEngine.Object[] assets;
  224. using (AssetsLoaderAsync assetsLoaderAsync = EntityFactory.Create<AssetsLoaderAsync, AssetBundle>(assetBundle))
  225. {
  226. assets = await assetsLoaderAsync.LoadAllAssetsAsync();
  227. }
  228. foreach (UnityEngine.Object asset in assets)
  229. {
  230. string path = $"{assetBundleName}/{asset.name}".ToLower();
  231. this.resourceCache[path] = asset;
  232. }
  233. }
  234. this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
  235. }
  236. public override void Dispose()
  237. {
  238. if (this.Id == 0)
  239. {
  240. return;
  241. }
  242. base.Dispose();
  243. foreach (var abInfo in this.bundles)
  244. {
  245. abInfo.Value?.AssetBundle?.Unload(true);
  246. }
  247. this.bundles.Clear();
  248. this.cacheDictionary.Clear();
  249. this.resourceCache.Clear();
  250. }
  251. }
  252. }