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."); } } /// /// 转换AddressableName /// 会根据游戏当前语言、品质等级返回对应的AddressableName /// /// /// public static string ConvertAddressableName(string addressableName) { if (_assetMgr != null) { return _assetMgr.ConvertAddressableName(addressableName); } return addressableName; } /// /// 同步加载接口 /// 非必要不要使用 /// webgl、微信小游戏不能使用 /// 使用该接口加载的资源严禁使用Object.Destroy销毁,只能使用Recycle(obj)方法回收 /// /// 返回值类型 /// 可寻址资源名字 /// 返回泛型对象 public static TResult LoadSync(string addressableName) { #if UNITY_EDITOR if (TryLoadSyncForEditor(addressableName, out TResult result)) { return result; } #endif return Instance.LoadSync(addressableName); } /// /// 异步加载接口 /// 使用该接口加载的资源严禁使用Object.Destroy销毁,只能使用Recycle(obj)方法回收 /// /// 返回对象类型 /// 可寻址资源名字 /// 返回异步回调 public static IAssetLoadAsync LoadAsync(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(addressableName); } /// /// 加载Scene的AssetBundle到内存 /// /// 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(IAssetLoadAsync async) { Instance.StopAsync(async); } /// /// 卸载当前未使用的资源 /// public static void UnloadUnusedAssets() { Instance.UnloadUnusedAssets(); } /// /// 销毁manager /// /// public static IAsync Dispose() { Log.Debug($"AssetManager Dispose. {_assetMgr != null}"); if (_assetMgr != null) { var async = _assetMgr.Dispose(); _assetMgr = null; //async.On((aAsync => { _assetMgr = null; })); return async; } return null; } /// /// 销毁旧的,重新初始化manager /// /// 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(string addressableName, out TResult result) { if (UnityEngine.Application.isPlaying == false) { if (LoaderHandler != null) { result = LoaderHandler.LoadSync(addressableName); } else { result = default(TResult); } return true; } result = default; return false; } #endif } }