AssetManager.static.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using XGame.Framework;
  2. using XGame.Framework.Asyncs;
  3. using XGame.Framework.i18n;
  4. using XGame.Framework.Quality;
  5. #if UNITY_EDITOR
  6. using System.Linq;
  7. #endif
  8. namespace XGame.Framework.Asset
  9. {
  10. public sealed partial class AssetManager
  11. {
  12. private static IAssetManager _assetMgr;
  13. private static IAssetManager Instance => _assetMgr ?? (_assetMgr = new AssetManager());
  14. public static void ChangedLanguage(LanguageType langFlag)
  15. {
  16. if (_assetMgr != null && _assetMgr is AssetManager manager)
  17. {
  18. manager.SetLangueType(langFlag);
  19. }
  20. else
  21. {
  22. AssetsLog.Error($"IAssetManager has dispose. But ChangedLanguage Event callback.");
  23. }
  24. }
  25. public static void ChangeQualityLevel(XQualityLevel quality)
  26. {
  27. if (_assetMgr != null && _assetMgr is AssetManager manager)
  28. {
  29. manager.SetQuality(quality);
  30. }
  31. else
  32. {
  33. AssetsLog.Error($"IAssetManager had dispose. But ChangeQualityLevel Event callback.");
  34. }
  35. }
  36. /// <summary>
  37. /// 转换AddressableName
  38. /// 会根据游戏当前语言、品质等级返回对应的AddressableName
  39. /// </summary>
  40. /// <param name="addressableName"></param>
  41. /// <returns></returns>
  42. public static string ConvertAddressableName(string addressableName)
  43. {
  44. if (_assetMgr != null)
  45. {
  46. return _assetMgr.ConvertAddressableName(addressableName);
  47. }
  48. return addressableName;
  49. }
  50. /// <summary>
  51. /// 同步加载接口
  52. /// 非必要不要使用
  53. /// 使用该接口加载的资源严禁使用Object.Destroy销毁,只能使用Recycle(obj)方法回收
  54. /// </summary>
  55. /// <typeparam name="TResult">返回值类型</typeparam>
  56. /// <param name="addressableName">可寻址资源名字</param>
  57. /// <returns>返回泛型对象</returns>
  58. public static TResult LoadSync<TResult>(string addressableName)
  59. {
  60. #if UNITY_EDITOR
  61. if (TryLoadSyncForEditor(addressableName, out TResult result))
  62. {
  63. return result;
  64. }
  65. #endif
  66. return Instance.LoadSync<TResult>(addressableName);
  67. }
  68. /// <summary>
  69. /// 异步加载接口
  70. /// 使用该接口加载的资源严禁使用Object.Destroy销毁,只能使用Recycle(obj)方法回收
  71. /// </summary>
  72. /// <typeparam name="TResult">返回对象类型</typeparam>
  73. /// <param name="addressableName">可寻址资源名字</param>
  74. /// <returns>返回异步回调</returns>
  75. public static IAssetLoadAsync<TResult> LoadAsync<TResult>(string addressableName)
  76. {
  77. #if UNITY_EDITOR
  78. Assert.IsTrue(UnityEngine.Application.isPlaying, "非运行时加载资源请使用 AddressableHelper.LoadAssetByAddressableName()");
  79. #endif
  80. return Instance.LoadAsync<TResult>(addressableName);
  81. }
  82. /// <summary>
  83. /// 加载Scene的AssetBundle到内存
  84. /// </summary>
  85. /// <param name="bundleName"></param>
  86. public static IAsync LoadSceneBundle(string sceneName)
  87. {
  88. return Instance.LoadSceneBundle(sceneName);
  89. }
  90. public static void UnLoadSceneBundle(string sceneName)
  91. {
  92. Instance.UnLoadSceneBundle(sceneName);
  93. }
  94. /// <summary>
  95. /// 回收资源
  96. /// </summary>
  97. /// <param name="obj"></param>
  98. public static void Recycle(object obj)
  99. {
  100. #if UNITY_EDITOR
  101. if (!UnityEngine.Application.isPlaying)
  102. {
  103. return;
  104. }
  105. #endif
  106. Instance.Recycle(obj);
  107. }
  108. /// <summary>
  109. /// 停止指定的异步加载
  110. /// </summary>
  111. /// <typeparam name="TResult"></typeparam>
  112. /// <param name="async"></param>
  113. public static void StopAsync<TResult>(IAssetLoadAsync<TResult> async)
  114. {
  115. Instance.StopAsync(async);
  116. }
  117. /// <summary>
  118. /// 卸载当前未使用的资源
  119. /// </summary>
  120. public static void UnloadUnusedAssets()
  121. {
  122. Instance.UnloadUnusedAssets();
  123. }
  124. public static IAsync Dispose()
  125. {
  126. if (_assetMgr != null)
  127. {
  128. var async = _assetMgr.Dispose();
  129. async.On((aAsync => { _assetMgr = null; }));
  130. return async;
  131. }
  132. return null;
  133. }
  134. /// <summary>
  135. /// 设置资源的预加载标记
  136. /// </summary>
  137. /// <param name="addressableName"></param>
  138. /// <param name="isPreload"></param>
  139. public static void SetPreloadTag(string addressableName, bool isPreload)
  140. {
  141. if (_assetMgr is IAssetCacheHandle handle)
  142. {
  143. handle.SetPreloadTag(addressableName, isPreload);
  144. }
  145. }
  146. #if UNITY_EDITOR
  147. private static IAssetLoadHandler _loadHandler;
  148. private static IAssetLoadHandler LoaderHandler
  149. {
  150. get
  151. {
  152. if (_loadHandler == null)
  153. {
  154. var hostEditorName = "Framework.Editor";
  155. var adapterType = typeof(IAssetLoadHandler);
  156. var handlerImplType = System.AppDomain.CurrentDomain.GetAssemblies()
  157. .First(assembly => assembly.GetName().Name.Equals(hostEditorName))
  158. .GetTypes().First(type => adapterType.IsAssignableFrom(type));
  159. if (handlerImplType != null)
  160. {
  161. _loadHandler = System.Activator.CreateInstance(handlerImplType) as IAssetLoadHandler;
  162. }
  163. else
  164. {
  165. UnityEngine.Debug.LogError("没有找到 IAssetLoaderHandler 的实现类。");
  166. }
  167. }
  168. return _loadHandler;
  169. }
  170. }
  171. private static bool TryLoadSyncForEditor<TResult>(string addressableName, out TResult result)
  172. {
  173. if (UnityEngine.Application.isPlaying == false)
  174. {
  175. if (LoaderHandler != null)
  176. {
  177. result = LoaderHandler.LoadSync<TResult>(addressableName);
  178. }
  179. else
  180. {
  181. result = default(TResult);
  182. }
  183. return true;
  184. }
  185. result = default;
  186. return false;
  187. }
  188. #endif
  189. }
  190. }