AssetBundleLoader.Async.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using XGame.Framework.Asyncs;
  2. using XGame.Framework.Utils;
  3. using UnityEngine;
  4. namespace XGame.Framework.Asset
  5. {
  6. /// <summary>
  7. /// AssetBundle的加载方式,需要先实现Bundle的Manifest管理
  8. /// </summary>
  9. internal partial class AssetBundleLoader
  10. {
  11. /// <summary>
  12. /// 创建从Repository获取Asset的异步
  13. /// Repository没缓存时则返回Null
  14. /// </summary>
  15. /// <param name="addressableName"></param>
  16. /// <returns></returns>
  17. AssetLoadFromCacheAsync CreateAssetFromCacheAsync(string addressableName)
  18. {
  19. var source = Repository.PopUnusedAsset(addressableName);
  20. if (source != null)
  21. {
  22. var assetAsync = new AssetLoadFromCacheAsync()
  23. {
  24. AddressableName = addressableName,
  25. Source = source,
  26. };
  27. return assetAsync;
  28. }
  29. return null;
  30. }
  31. ///// <summary>
  32. ///// 创建加载Asset的异步
  33. ///// </summary>
  34. ///// <param name="assetPath"></param>
  35. ///// <returns></returns>
  36. //AssetLoadFromBundleAsync CreateAssetLoadFromBundleAsync(string assetPath)
  37. //{
  38. // var assetAsync = AsyncPool.GetAssetAsync(assetPath) as AssetLoadFromBundleAsync;
  39. // if (assetAsync == null)
  40. // {
  41. // assetAsync = new AssetLoadFromBundleAsync
  42. // {
  43. // Path = assetPath
  44. // };
  45. // //记录当前加载的AssetPath
  46. // LoadingAssets.Add(assetPath);
  47. // assetAsync.On(OnAssetCompleted);
  48. // }
  49. // return assetAsync;
  50. //}
  51. /// <summary>
  52. /// 创建加载Bundle的异步
  53. /// </summary>
  54. /// <param name="bundleName"></param>
  55. /// <returns></returns>
  56. IAssetAsync CreateBundleAsync(string bundleName)
  57. {
  58. //var path = FileUtil.GetAssetBundlePath(bundleName);
  59. if (Repository.ContainsBundle(bundleName))
  60. return null;
  61. var assetAsync = AsyncPool.GetAssetAsync(bundleName);
  62. if (assetAsync != null)
  63. return assetAsync;
  64. #if UNITY_EDITOR || !(UNITY_WEBGL || UNITY_WEIXINMINIGAME)
  65. var requestAsync = new AssetBundleCreateRequestAsync
  66. {
  67. Path = FileUtils.GetAssetBundlePath(bundleName),
  68. AddressableName = bundleName,
  69. Offset = GetBundleOffest(bundleName)
  70. };
  71. #else
  72. var requestAsync = new WebRequestAssetBundleAsync
  73. {
  74. Path = FileUtils.GetAssetBundlePath(bundleName),
  75. AddressableName = bundleName,
  76. Offset = GetBundleOffest(bundleName)
  77. };
  78. #endif
  79. requestAsync.On(OnBundleCompleted);
  80. //AsyncPool.AddAsync(requestAsync);
  81. return requestAsync;
  82. }
  83. /// <summary>
  84. /// AssetBundleCreateRequestAsync的回调监听
  85. /// </summary>
  86. /// <param name="async"></param>
  87. void OnBundleCompleted(IAsync async)
  88. {
  89. if (async is IAssetAsync assetAsync)
  90. {
  91. var bundleName = assetAsync.AddressableName;
  92. var result = assetAsync.GetResult();
  93. var bundleAsset = Repository.GetBundle(bundleName);
  94. if (bundleAsset != null)
  95. {
  96. //bundleAsset.Retain();
  97. AssetsLog.Warn("Bundle load repeated. Name:{0} Last:{1} Next:{2}", bundleName, bundleAsset.Source.GetLongHashCode(), result?.GetLongHashCode());
  98. return;
  99. }
  100. bundleAsset = AssetCreator.Create(bundleName, result) as BundleAsset;
  101. if (bundleAsset != null)
  102. {
  103. Repository.AddBundle(bundleAsset);
  104. }
  105. else
  106. {
  107. AssetsLog.Error("Load bundle failed. Bundle:{0}", bundleName);
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// AssetLoadFromBundleAsync的回调监听
  113. /// </summary>
  114. /// <param name="async"></param>
  115. void OnAssetCompleted(IAsync async)
  116. {
  117. if (async is AssetLoadFromBundleAsync assetAsync)
  118. {
  119. int assetHash = assetAsync.AssetHash;
  120. var assetName = assetAsync.AddressableName;
  121. //删除当前加载的AssetPath
  122. //LoadingAssets.Remove(assetPath);
  123. if (assetHash == 0)
  124. {
  125. AssetsLog.Error($"AssetBundle load failed. Asset is null. Name:{assetName}");
  126. return;
  127. }
  128. //记录hash
  129. if (!Repository.RegistAssetHash(assetHash, assetName))
  130. {
  131. //发现重复的Hash记录,不需要再重复记录Bundle的引用计数
  132. //出现重复的原因是异步加载Asset后又使用同步加载了同一个Asset
  133. return;
  134. }
  135. if (TryGetBundleInfo(assetName, out string bundleName, out string[] dependencies))
  136. {
  137. //增加依赖bundle的引用计数
  138. //string bundlePath = FileUtil.GetAssetBundlePath(bundleName);
  139. Repository.RetainBundle(bundleName);
  140. if (dependencies != null)
  141. {
  142. foreach (var dep in dependencies)
  143. {
  144. //var depBundlePath = FileUtil.GetAssetBundlePath(dep);
  145. Repository.RetainBundle(dep);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// 异步加载scene的回调
  153. /// </summary>
  154. /// <param name="async"></param>
  155. void OnSceneComplete(IAsync async)
  156. {
  157. if (async is SceneLoadAsync sceneAsync)
  158. {
  159. var assetName = sceneAsync.AddressableName;
  160. if (TryGetBundleInfo(assetName, out string bundleName, out string[] dependencies))
  161. {
  162. //sceneBundle在这里添加到Reponsitory
  163. var sceneBundle = Repository.GetBundle(bundleName);
  164. if (sceneBundle != null)
  165. {
  166. AssetsLog.Warn("Bundle load repeated. Name:{0} Last:{1}", bundleName, sceneBundle.Source.GetLongHashCode());
  167. return;
  168. }
  169. //更新引用计数
  170. Repository.RetainBundle(bundleName);
  171. if (dependencies != null)
  172. {
  173. foreach (var dep in dependencies)
  174. {
  175. Repository.RetainBundle(dep);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. AssetBundle GetAssetBundleByName(string bundleName)
  182. {
  183. var bundleAsset = Repository.GetBundle(bundleName);
  184. return bundleAsset?.Source as AssetBundle;
  185. }
  186. }
  187. }