123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using XGame.Framework.Asyncs;
- using XGame.Framework.i18n;
- using XGame.Framework.Quality;
- #if UNITY_EDITOR
- using System.Linq;
- #endif
- namespace XGame.Framework.Asset
- {
- public sealed partial class AssetManager
- {
- private static IAssetManager _assetMgr;
- private static IAssetManager Instance => _assetMgr;
- public static IAsync Init()
- {
- var loaderAsync = new LoaderBuildAsync();
- loaderAsync.Start();
- loaderAsync.On(_ =>
- {
- var loader = loaderAsync.Result;
- if (loader != null)
- _assetMgr = new AssetManager(loader);
- Log.Debug($"LoaderBuildAsync completed. loader:{_assetMgr != null}");
- });
- return loaderAsync;
- }
- public static void ChangedLanguage(LanguageType langFlag)
- {
- if (_assetMgr != null && _assetMgr is AssetManager manager)
- {
- manager.SetLangueType(langFlag);
- }
- else
- {
- AssetsLog.Error($"IAssetManager has dispose. But ChangedLanguage Event callback.");
- }
- }
- public static void ChangeQualityLevel(XQualityLevel quality)
- {
- if (_assetMgr != null && _assetMgr is AssetManager manager)
- {
- manager.SetQuality(quality);
- }
- else
- {
- AssetsLog.Error($"IAssetManager had dispose. But ChangeQualityLevel Event callback.");
- }
- }
-
-
-
-
-
-
- public static string ConvertAddressableName(string addressableName)
- {
- if (_assetMgr != null)
- {
- return _assetMgr.ConvertAddressableName(addressableName);
- }
- return addressableName;
- }
-
-
-
-
-
-
-
-
-
- public static TResult LoadSync<TResult>(string addressableName)
- {
- #if UNITY_EDITOR
- if (TryLoadSyncForEditor(addressableName, out TResult result))
- {
- return result;
- }
- #endif
- return Instance.LoadSync<TResult>(addressableName);
- }
-
-
-
-
-
-
-
- public static IAssetLoadAsync<TResult> LoadAsync<TResult>(string addressableName)
- {
- #if UNITY_EDITOR
- Assert.IsTrue(UnityEngine.Application.isPlaying, "非运行时加载资源请使用 AddressableHelper.LoadAssetByAddressableName()");
- Assert.IsTrue(_assetMgr != null, $"AssetManager没有实例化. Thread:{System.Threading.Thread.CurrentThread.ManagedThreadId}");
- #endif
- return Instance.LoadAsync<TResult>(addressableName);
- }
-
-
-
-
- public static IAsync LoadSceneBundle(string sceneName)
- {
- return Instance.LoadSceneBundle(sceneName);
- }
- public static void UnLoadSceneBundle(string sceneName)
- {
- Instance.UnLoadSceneBundle(sceneName);
- }
-
-
-
-
- public static void Recycle(object obj)
- {
- #if UNITY_EDITOR
- if (!UnityEngine.Application.isPlaying)
- {
- return;
- }
- #endif
- Instance.Recycle(obj);
- }
-
-
-
-
-
- public static void StopAsync<TResult>(IAssetLoadAsync<TResult> async)
- {
- Instance.StopAsync(async);
- }
-
-
-
- public static void UnloadUnusedAssets()
- {
- Instance.UnloadUnusedAssets();
- }
-
-
-
-
- public static IAsync Dispose()
- {
- Log.Debug($"AssetManager Dispose. {_assetMgr != null}");
- if (_assetMgr != null)
- {
- var async = _assetMgr.Dispose();
- _assetMgr = null;
-
- return async;
- }
- return null;
- }
-
-
-
-
- public static IAsync ReStart()
- {
- var asyncGroup = new AsyncGroup();
- var loaderAsync = new LoaderBuildAsync();
- if (_assetMgr != null)
- {
- var disposeAsync = _assetMgr.Dispose();
- disposeAsync.Join(asyncGroup);
- disposeAsync.On(_ =>
- {
- _assetMgr = null;
- loaderAsync.Start();
- });
- }
- else
- {
- loaderAsync.Start();
- }
- loaderAsync.Join(asyncGroup);
- loaderAsync.On(_ =>
- {
- var loader = loaderAsync.Result;
- if (loader != null)
- _assetMgr = new AssetManager(loader);
- });
- asyncGroup.End();
- return asyncGroup;
- }
-
-
-
-
-
- public static void SetPreloadTag(string addressableName, bool isPreload)
- {
- if (_assetMgr is IAssetCacheHandle handle)
- {
- handle.SetPreloadTag(addressableName, isPreload);
- }
- }
- #if UNITY_EDITOR
- private static IAssetLoadHandler _loadHandler;
- private static IAssetLoadHandler LoaderHandler
- {
- get
- {
- if (_loadHandler == null)
- {
- var hostEditorName = "Framework.Editor";
- var adapterType = typeof(IAssetLoadHandler);
- var handlerImplType = System.AppDomain.CurrentDomain.GetAssemblies()
- .First(assembly => assembly.GetName().Name.Equals(hostEditorName))
- .GetTypes().First(type => adapterType.IsAssignableFrom(type));
- if (handlerImplType != null)
- {
- _loadHandler = System.Activator.CreateInstance(handlerImplType) as IAssetLoadHandler;
- }
- else
- {
- UnityEngine.Debug.LogError("没有找到 IAssetLoaderHandler 的实现类。");
- }
- }
- return _loadHandler;
- }
- }
- private static bool TryLoadSyncForEditor<TResult>(string addressableName, out TResult result)
- {
- if (UnityEngine.Application.isPlaying == false)
- {
- if (LoaderHandler != null)
- {
- result = LoaderHandler.LoadSync<TResult>(addressableName);
- }
- else
- {
- result = default(TResult);
- }
- return true;
- }
- result = default;
- return false;
- }
- #endif
- }
- }
|