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.");
- }
- }
- /// <summary>
- /// 转换AddressableName
- /// 会根据游戏当前语言、品质等级返回对应的AddressableName
- /// </summary>
- /// <param name="addressableName"></param>
- /// <returns></returns>
- public static string ConvertAddressableName(string addressableName)
- {
- if (_assetMgr != null)
- {
- return _assetMgr.ConvertAddressableName(addressableName);
- }
- return addressableName;
- }
- /// <summary>
- /// 同步加载接口
- /// 非必要不要使用
- /// webgl、微信小游戏不能使用
- /// 使用该接口加载的资源严禁使用Object.Destroy销毁,只能使用Recycle(obj)方法回收
- /// </summary>
- /// <typeparam name="TResult">返回值类型</typeparam>
- /// <param name="addressableName">可寻址资源名字</param>
- /// <returns>返回泛型对象</returns>
- public static TResult LoadSync<TResult>(string addressableName)
- {
- #if UNITY_EDITOR
- if (TryLoadSyncForEditor(addressableName, out TResult result))
- {
- return result;
- }
- #endif
- return Instance.LoadSync<TResult>(addressableName);
- }
- /// <summary>
- /// 异步加载接口
- /// 使用该接口加载的资源严禁使用Object.Destroy销毁,只能使用Recycle(obj)方法回收
- /// </summary>
- /// <typeparam name="TResult">返回对象类型</typeparam>
- /// <param name="addressableName">可寻址资源名字</param>
- /// <returns>返回异步回调</returns>
- 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);
- }
- /// <summary>
- /// 加载Scene的AssetBundle到内存
- /// </summary>
- /// <param name="bundleName"></param>
- public static IAsync LoadSceneBundle(string sceneName)
- {
- return Instance.LoadSceneBundle(sceneName);
- }
- public static void UnLoadSceneBundle(string sceneName)
- {
- Instance.UnLoadSceneBundle(sceneName);
- }
- /// <summary>
- /// 回收资源
- /// </summary>
- /// <param name="obj"></param>
- public static void Recycle(object obj)
- {
- #if UNITY_EDITOR
- if (!UnityEngine.Application.isPlaying)
- {
- return;
- }
- #endif
- Instance.Recycle(obj);
- }
- /// <summary>
- /// 停止指定的异步加载
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="async"></param>
- public static void StopAsync<TResult>(IAssetLoadAsync<TResult> async)
- {
- Instance.StopAsync(async);
- }
- /// <summary>
- /// 卸载当前未使用的资源
- /// </summary>
- public static void UnloadUnusedAssets()
- {
- Instance.UnloadUnusedAssets();
- }
- /// <summary>
- /// 销毁manager
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 销毁旧的,重新初始化manager
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 设置资源的预加载标记
- /// </summary>
- /// <param name="addressableName"></param>
- /// <param name="isPreload"></param>
- 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
- }
- }
|