ResourcesComponent.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 ETModel
  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.IsDisposed)
  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.IsDisposed)
  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 UnityEngine.Object GetAsset(string bundleName, string prefab)
  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. if (resource == null)
  79. {
  80. throw new Exception($"asset type error, path: {path}");
  81. }
  82. return resource;
  83. }
  84. public void UnloadBundle(string assetBundleName)
  85. {
  86. assetBundleName = assetBundleName.ToLower();
  87. string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
  88. //Log.Debug($"-----------dep unload {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  89. foreach (string dependency in dependencies)
  90. {
  91. this.UnloadOneBundle(dependency);
  92. }
  93. }
  94. private void UnloadOneBundle(string assetBundleName)
  95. {
  96. assetBundleName = assetBundleName.ToLower();
  97. //Log.Debug($"unload bundle {assetBundleName}");
  98. ABInfo abInfo;
  99. if (!this.bundles.TryGetValue(assetBundleName, out abInfo))
  100. {
  101. throw new Exception($"not found assetBundle: {assetBundleName}");
  102. }
  103. --abInfo.RefCount;
  104. if (abInfo.RefCount > 0)
  105. {
  106. return;
  107. }
  108. this.bundles.Remove(assetBundleName);
  109. // 缓存10个包
  110. this.cacheDictionary.Enqueue(assetBundleName, abInfo);
  111. if (this.cacheDictionary.Count > 10)
  112. {
  113. abInfo = this.cacheDictionary[this.cacheDictionary.FirstKey];
  114. this.cacheDictionary.Dequeue();
  115. abInfo.Dispose();
  116. }
  117. //Log.Debug($"cache count: {this.cacheDictionary.Count}");
  118. }
  119. /// <summary>
  120. /// 同步加载assetbundle
  121. /// </summary>
  122. /// <param name="assetBundleName"></param>
  123. /// <returns></returns>
  124. public void LoadBundle(string assetBundleName)
  125. {
  126. assetBundleName = assetBundleName.ToLower();
  127. string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
  128. Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  129. foreach (string dependency in dependencies)
  130. {
  131. if (string.IsNullOrEmpty(dependency))
  132. {
  133. continue;
  134. }
  135. this.LoadOneBundle(dependency);
  136. }
  137. }
  138. public void LoadOneBundle(string assetBundleName)
  139. {
  140. ABInfo abInfo;
  141. if (this.bundles.TryGetValue(assetBundleName, out abInfo))
  142. {
  143. ++abInfo.RefCount;
  144. return;
  145. }
  146. if (this.cacheDictionary.ContainsKey(assetBundleName))
  147. {
  148. abInfo = this.cacheDictionary[assetBundleName];
  149. ++abInfo.RefCount;
  150. this.bundles[assetBundleName] = abInfo;
  151. this.cacheDictionary.Remove(assetBundleName);
  152. return;
  153. }
  154. if (!Define.IsAsync)
  155. {
  156. string[] realPath = null;
  157. #if UNITY_EDITOR
  158. realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  159. foreach (string s in realPath)
  160. {
  161. string assetName = Path.GetFileNameWithoutExtension(s);
  162. string path = $"{assetBundleName}/{assetName}".ToLower();
  163. UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
  164. this.resourceCache[path] = resource;
  165. }
  166. this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
  167. #endif
  168. return;
  169. }
  170. AssetBundle assetBundle = AssetBundle.LoadFromFile(Path.Combine(PathHelper.AppHotfixResPath, assetBundleName));
  171. if (!assetBundle.isStreamedSceneAssetBundle)
  172. {
  173. // 异步load资源到内存cache住
  174. UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
  175. foreach (UnityEngine.Object asset in assets)
  176. {
  177. string path = $"{assetBundleName}/{asset.name}".ToLower();
  178. this.resourceCache[path] = asset;
  179. }
  180. }
  181. this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
  182. }
  183. /// <summary>
  184. /// 异步加载assetbundle
  185. /// </summary>
  186. /// <param name="assetBundleName"></param>
  187. /// <returns></returns>
  188. public async Task LoadBundleAsync(string assetBundleName)
  189. {
  190. assetBundleName = assetBundleName.ToLower();
  191. string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
  192. //Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  193. foreach (string dependency in dependencies)
  194. {
  195. if (string.IsNullOrEmpty(dependency))
  196. {
  197. continue;
  198. }
  199. await this.LoadOneBundleAsync(dependency);
  200. }
  201. }
  202. public async Task LoadOneBundleAsync(string assetBundleName)
  203. {
  204. ABInfo abInfo;
  205. if (this.bundles.TryGetValue(assetBundleName, out abInfo))
  206. {
  207. ++abInfo.RefCount;
  208. return;
  209. }
  210. if (this.cacheDictionary.ContainsKey(assetBundleName))
  211. {
  212. abInfo = this.cacheDictionary[assetBundleName];
  213. ++abInfo.RefCount;
  214. this.bundles[assetBundleName] = abInfo;
  215. this.cacheDictionary.Remove(assetBundleName);
  216. return;
  217. }
  218. if (!Define.IsAsync)
  219. {
  220. string[] realPath = null;
  221. #if UNITY_EDITOR
  222. realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  223. foreach (string s in realPath)
  224. {
  225. string assetName = Path.GetFileNameWithoutExtension(s);
  226. string path = $"{assetBundleName}/{assetName}".ToLower();
  227. UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
  228. this.resourceCache[path] = resource;
  229. }
  230. this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
  231. #endif
  232. return;
  233. }
  234. AssetBundle assetBundle;
  235. using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = ComponentFactory.Create<AssetsBundleLoaderAsync>())
  236. {
  237. assetBundle = await assetsBundleLoaderAsync.LoadAsync(assetBundleName);
  238. }
  239. if (!assetBundle.isStreamedSceneAssetBundle)
  240. {
  241. // 异步load资源到内存cache住
  242. UnityEngine.Object[] assets;
  243. using (AssetsLoaderAsync assetsLoaderAsync = ComponentFactory.Create<AssetsLoaderAsync, AssetBundle>(assetBundle))
  244. {
  245. assets = await assetsLoaderAsync.LoadAllAssetsAsync();
  246. }
  247. foreach (UnityEngine.Object asset in assets)
  248. {
  249. string path = $"{assetBundleName}/{asset.name}".ToLower();
  250. this.resourceCache[path] = asset;
  251. }
  252. }
  253. this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
  254. }
  255. public string DebugString()
  256. {
  257. StringBuilder sb = new StringBuilder();
  258. foreach (ABInfo abInfo in this.bundles.Values)
  259. {
  260. sb.Append($"{abInfo.Name}:{abInfo.RefCount}\n");
  261. }
  262. return sb.ToString();
  263. }
  264. }
  265. }