ResourcesComponent.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 : Component
  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($"destroy 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. while (cacheDictionary.Count > 0)
  67. {
  68. ABInfo abInfo = this.cacheDictionary.FirstValue;
  69. this.cacheDictionary.Dequeue();
  70. abInfo.AssetBundle?.Unload(true);
  71. }
  72. this.bundles.Clear();
  73. this.cacheDictionary.Clear();
  74. this.resourceCache.Clear();
  75. }
  76. public UnityEngine.Object GetAsset(string bundleName, string prefab)
  77. {
  78. string path = $"{bundleName}/{prefab}".ToLower();
  79. UnityEngine.Object resource = null;
  80. if (!this.resourceCache.TryGetValue(path, out resource))
  81. {
  82. throw new Exception($"not found asset: {path}");
  83. }
  84. return resource;
  85. }
  86. public void UnloadBundle(string assetBundleName)
  87. {
  88. assetBundleName = assetBundleName.ToLower();
  89. string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
  90. //Log.Debug($"-----------dep unload {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  91. foreach (string dependency in dependencies)
  92. {
  93. this.UnloadOneBundle(dependency);
  94. }
  95. }
  96. private void UnloadOneBundle(string assetBundleName)
  97. {
  98. assetBundleName = assetBundleName.ToLower();
  99. ABInfo abInfo;
  100. if (!this.bundles.TryGetValue(assetBundleName, out abInfo))
  101. {
  102. throw new Exception($"not found assetBundle: {assetBundleName}");
  103. }
  104. //Log.Debug($"---------- unload one bundle {assetBundleName} refcount: {abInfo.RefCount}");
  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. }
  140. public void LoadOneBundle(string assetBundleName)
  141. {
  142. //Log.Debug($"---------------load one bundle {assetBundleName}");
  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. string[] realPath = null;
  160. #if UNITY_EDITOR
  161. realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  162. foreach (string s in realPath)
  163. {
  164. string assetName = Path.GetFileNameWithoutExtension(s);
  165. string path = $"{assetBundleName}/{assetName}".ToLower();
  166. UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
  167. this.resourceCache[path] = resource;
  168. }
  169. this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
  170. #endif
  171. return;
  172. }
  173. string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
  174. AssetBundle assetBundle = null;
  175. if (File.Exists(p))
  176. {
  177. assetBundle = AssetBundle.LoadFromFile(p);
  178. }
  179. else
  180. {
  181. p = Path.Combine(PathHelper.AppResPath, assetBundleName);
  182. assetBundle = AssetBundle.LoadFromFile(p);
  183. }
  184. if (assetBundle == null)
  185. {
  186. throw new Exception($"assets bundle not found: {assetBundleName}");
  187. }
  188. if (!assetBundle.isStreamedSceneAssetBundle)
  189. {
  190. // 异步load资源到内存cache住
  191. UnityEngine.Object[] assets = assetBundle.LoadAllAssets();
  192. foreach (UnityEngine.Object asset in assets)
  193. {
  194. string path = $"{assetBundleName}/{asset.name}".ToLower();
  195. this.resourceCache[path] = asset;
  196. }
  197. }
  198. this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
  199. }
  200. /// <summary>
  201. /// 异步加载assetbundle
  202. /// </summary>
  203. /// <param name="assetBundleName"></param>
  204. /// <returns></returns>
  205. public async Task LoadBundleAsync(string assetBundleName)
  206. {
  207. assetBundleName = assetBundleName.ToLower();
  208. string[] dependencies = ResourcesHelper.GetSortedDependencies(assetBundleName);
  209. //Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
  210. foreach (string dependency in dependencies)
  211. {
  212. if (string.IsNullOrEmpty(dependency))
  213. {
  214. continue;
  215. }
  216. await this.LoadOneBundleAsync(dependency);
  217. }
  218. }
  219. public async Task LoadOneBundleAsync(string assetBundleName)
  220. {
  221. //Log.Debug($"---------------load one bundle {assetBundleName}");
  222. ABInfo abInfo;
  223. if (this.bundles.TryGetValue(assetBundleName, out abInfo))
  224. {
  225. ++abInfo.RefCount;
  226. return;
  227. }
  228. if (this.cacheDictionary.ContainsKey(assetBundleName))
  229. {
  230. abInfo = this.cacheDictionary[assetBundleName];
  231. ++abInfo.RefCount;
  232. this.bundles[assetBundleName] = abInfo;
  233. this.cacheDictionary.Remove(assetBundleName);
  234. return;
  235. }
  236. if (!Define.IsAsync)
  237. {
  238. string[] realPath = null;
  239. #if UNITY_EDITOR
  240. realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  241. foreach (string s in realPath)
  242. {
  243. string assetName = Path.GetFileNameWithoutExtension(s);
  244. string path = $"{assetBundleName}/{assetName}".ToLower();
  245. UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s);
  246. this.resourceCache[path] = resource;
  247. }
  248. this.bundles[assetBundleName] = new ABInfo(assetBundleName, null);
  249. #endif
  250. return;
  251. }
  252. string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
  253. AssetBundle assetBundle = null;
  254. if (File.Exists(p))
  255. {
  256. assetBundle = AssetBundle.LoadFromFile(p);
  257. }
  258. else
  259. {
  260. p = Path.Combine(PathHelper.AppResPath, assetBundleName);
  261. assetBundle = AssetBundle.LoadFromFile(p);
  262. }
  263. if (assetBundle == null)
  264. {
  265. throw new Exception($"assets bundle not found: {assetBundleName}");
  266. }
  267. if (!assetBundle.isStreamedSceneAssetBundle)
  268. {
  269. // 异步load资源到内存cache住
  270. UnityEngine.Object[] assets;
  271. using (AssetsLoaderAsync assetsLoaderAsync = ComponentFactory.Create<AssetsLoaderAsync, AssetBundle>(assetBundle))
  272. {
  273. assets = await assetsLoaderAsync.LoadAllAssetsAsync();
  274. }
  275. foreach (UnityEngine.Object asset in assets)
  276. {
  277. string path = $"{assetBundleName}/{asset.name}".ToLower();
  278. this.resourceCache[path] = asset;
  279. }
  280. }
  281. this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
  282. }
  283. public string DebugString()
  284. {
  285. StringBuilder sb = new StringBuilder();
  286. foreach (ABInfo abInfo in this.bundles.Values)
  287. {
  288. sb.Append($"{abInfo.Name}:{abInfo.RefCount}\n");
  289. }
  290. return sb.ToString();
  291. }
  292. }
  293. }