ResourcesComponent.cs 7.9 KB

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