AssetBundleLoader.Async.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. AssetBundleCreateRequestAsync requestAsync = new AssetBundleCreateRequestAsync
  65. {
  66. Path = FileUtils.GetAssetBundlePath(bundleName),
  67. AddressableName = bundleName,
  68. Offset = GetBundleOffest(bundleName)
  69. };
  70. requestAsync.On(OnBundleCompleted);
  71. //AsyncPool.AddAsync(requestAsync);
  72. return requestAsync;
  73. }
  74. /// <summary>
  75. /// AssetBundleCreateRequestAsync的回调监听
  76. /// </summary>
  77. /// <param name="async"></param>
  78. void OnBundleCompleted(IAsync async)
  79. {
  80. if (async is IAssetAsync assetAsync)
  81. {
  82. var bundleName = assetAsync.AddressableName;
  83. var result = assetAsync.GetResult();
  84. var bundleAsset = Repository.GetBundle(bundleName);
  85. if (bundleAsset != null)
  86. {
  87. //bundleAsset.Retain();
  88. AssetsLog.Warn("Bundle load repeated. Name:{0} Last:{1} Next:{2}", bundleName, bundleAsset.Source.GetLongHashCode(), result?.GetLongHashCode());
  89. return;
  90. }
  91. bundleAsset = AssetCreator.Create(bundleName, result) as BundleAsset;
  92. if (bundleAsset != null)
  93. {
  94. Repository.AddBundle(bundleAsset);
  95. }
  96. else
  97. {
  98. AssetsLog.Error("Load bundle failed. Bundle:{0}", bundleName);
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// AssetLoadFromBundleAsync的回调监听
  104. /// </summary>
  105. /// <param name="async"></param>
  106. void OnAssetCompleted(IAsync async)
  107. {
  108. if (async is AssetLoadFromBundleAsync assetAsync)
  109. {
  110. int assetHash = assetAsync.AssetHash;
  111. var assetName = assetAsync.AddressableName;
  112. //删除当前加载的AssetPath
  113. //LoadingAssets.Remove(assetPath);
  114. if (assetHash == 0)
  115. {
  116. AssetsLog.Error($"AssetBundle load failed. Asset is null. Name:{assetName}");
  117. return;
  118. }
  119. //记录hash
  120. if (!Repository.RegistAssetHash(assetHash, assetName))
  121. {
  122. //发现重复的Hash记录,不需要再重复记录Bundle的引用计数
  123. //出现重复的原因是异步加载Asset后又使用同步加载了同一个Asset
  124. return;
  125. }
  126. if (TryGetBundleInfo(assetName, out string bundleName, out string[] dependencies))
  127. {
  128. //增加依赖bundle的引用计数
  129. //string bundlePath = FileUtil.GetAssetBundlePath(bundleName);
  130. Repository.RetainBundle(bundleName);
  131. if (dependencies != null)
  132. {
  133. foreach (var dep in dependencies)
  134. {
  135. //var depBundlePath = FileUtil.GetAssetBundlePath(dep);
  136. Repository.RetainBundle(dep);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 异步加载scene的回调
  144. /// </summary>
  145. /// <param name="async"></param>
  146. void OnSceneComplete(IAsync async)
  147. {
  148. if (async is SceneLoadAsync sceneAsync)
  149. {
  150. var assetName = sceneAsync.AddressableName;
  151. if (TryGetBundleInfo(assetName, out string bundleName, out string[] dependencies))
  152. {
  153. //sceneBundle在这里添加到Reponsitory
  154. var sceneBundle = Repository.GetBundle(bundleName);
  155. if (sceneBundle != null)
  156. {
  157. AssetsLog.Warn("Bundle load repeated. Name:{0} Last:{1}", bundleName, sceneBundle.Source.GetLongHashCode());
  158. return;
  159. }
  160. //更新引用计数
  161. Repository.RetainBundle(bundleName);
  162. if (dependencies != null)
  163. {
  164. foreach (var dep in dependencies)
  165. {
  166. Repository.RetainBundle(dep);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. AssetBundle GetAssetBundleByName(string bundleName)
  173. {
  174. var bundleAsset = Repository.GetBundle(bundleName);
  175. return bundleAsset?.Source as AssetBundle;
  176. }
  177. }
  178. }