diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index ebb41dc..2745f6a 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.SceneManagement; namespace YooAsset { @@ -163,21 +164,18 @@ namespace YooAsset /// 异步加载场景 /// /// 场景名称 - public static AssetOperationHandle LoadSceneAsync(string scenePath, SceneInstanceParam instanceParam) + public static SceneOperationHandle LoadSceneAsync(string scenePath, LoadSceneMode mode, bool activateOnLoad) { AssetProviderBase provider = TryGetAssetProvider(scenePath); if (provider == null) { if (SimulationOnEditor) - provider = new DatabaseSceneProvider(scenePath, instanceParam); + provider = new DatabaseSceneProvider(scenePath, mode, activateOnLoad); else - provider = new BundledSceneProvider(scenePath, instanceParam); + provider = new BundledSceneProvider(scenePath, mode, activateOnLoad); _providers.Add(provider); } - - // 引用计数增加 - provider.Reference(); - return provider.Handle; + return provider.CreateHandle() as SceneOperationHandle; } /// @@ -196,10 +194,7 @@ namespace YooAsset provider = new BundledAssetProvider(assetPath, assetType); _providers.Add(provider); } - - // 引用计数增加 - provider.Reference(); - return provider.Handle; + return provider.CreateHandle() as AssetOperationHandle; } /// @@ -218,10 +213,7 @@ namespace YooAsset provider = new BundledSubAssetsProvider(assetPath, assetType); _providers.Add(provider); } - - // 引用计数增加 - provider.Reference(); - return provider.Handle; + return provider.CreateHandle() as AssetOperationHandle; } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance.meta b/Assets/YooAsset/Runtime/AssetSystem/Instance.meta deleted file mode 100644 index 06a9b1d..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: a9b18d6c47ddf9b4296619d3d50ff826 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetInstance.cs b/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetInstance.cs deleted file mode 100644 index eac555e..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetInstance.cs +++ /dev/null @@ -1,7 +0,0 @@ - -namespace YooAsset -{ - public interface IAssetInstance - { - } -} diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetInstance.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetInstance.cs.meta deleted file mode 100644 index ecd0841..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetInstance.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: e1d0cfc6a2d5b1f4d9b9ac8f93682497 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetParam.cs b/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetParam.cs deleted file mode 100644 index 367e529..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetParam.cs +++ /dev/null @@ -1,7 +0,0 @@ - -namespace YooAsset -{ - public interface IAssetParam - { - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance/SceneInstance.cs b/Assets/YooAsset/Runtime/AssetSystem/Instance/SceneInstance.cs deleted file mode 100644 index 92c08f8..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance/SceneInstance.cs +++ /dev/null @@ -1,63 +0,0 @@ -using UnityEngine; -using UnityEngine.SceneManagement; - -namespace YooAsset -{ - /// - /// 扩展的场景实例对象 - /// - public class SceneInstance : IAssetInstance - { - private readonly AsyncOperation _asyncOp; - - public SceneInstance(AsyncOperation op) - { - _asyncOp = op; - } - - /// - /// UnityEngine场景对象 - /// - public UnityEngine.SceneManagement.Scene Scene { internal set; get; } - - /// - /// 激活场景 - /// - public bool Activate() - { - if (Scene == null) - return false; - - if (Scene.IsValid() && Scene.isLoaded) - { - return SceneManager.SetActiveScene(Scene); - } - else - { - YooLogger.Warning($"Scene is invalid or not loaded : {Scene.name}"); - return false; - } - } - } - - /// - /// 加载场景实例对象需要提供的参数类 - /// - public class SceneInstanceParam : IAssetParam - { - /// - /// 加载模式 - /// - public LoadSceneMode LoadMode { set; get; } - - /// - /// 物理模式 - /// - //public LocalPhysicsMode PhysicsMode { set; get;} - - /// - /// 加载完毕时是否主动激活 - /// - public bool ActivateOnLoad { set; get; } - } -} diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance/SceneInstance.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Instance/SceneInstance.cs.meta deleted file mode 100644 index 9568688..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance/SceneInstance.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab6e7cef318cff4684ed733679bca0e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs index 13e8a64..a0363be 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs @@ -1,69 +1,19 @@ -using System.Collections; -using UnityEngine; +using UnityEngine; namespace YooAsset { - public struct AssetOperationHandle : IEnumerator + public class AssetOperationHandle : OperationHandleBase { - private IAssetProvider _provider; + private System.Action _callback; - internal AssetOperationHandle(IAssetProvider provider) + internal AssetOperationHandle(AssetProviderBase provider) : base(provider) { - _provider = provider; } - - /// - /// 句柄是否有效(AssetFileLoader销毁会导致所有句柄失效) - /// - public bool IsValid + internal override void InvokeCallback() { - get + if (IsValid) { - return _provider != null && _provider.IsValid; - } - } - - /// - /// 当前状态 - /// - public EOperationStatus Status - { - get - { - if (IsValid == false) - return EOperationStatus.None; - if (_provider.Status == AssetProviderBase.EStatus.Fail) - return EOperationStatus.Failed; - else if (_provider.Status == AssetProviderBase.EStatus.Success) - return EOperationStatus.Succeed; - else - return EOperationStatus.None; - } - } - - /// - /// 加载进度 - /// - public float Progress - { - get - { - if (IsValid == false) - return 0; - return _provider.Progress; - } - } - - /// - /// 是否加载完毕 - /// - public bool IsDone - { - get - { - if (IsValid == false) - return false; - return _provider.IsDone; + _callback?.Invoke(this); } } @@ -79,13 +29,13 @@ namespace YooAsset if (_provider.IsDone) value.Invoke(this); else - _provider.Callback += value; + _callback += value; } remove { if (IsValid == false) throw new System.Exception($"{nameof(AssetOperationHandle)} is invalid"); - _provider.Callback -= value; + _callback -= value; } } @@ -102,32 +52,6 @@ namespace YooAsset } } - /// - /// 资源对象集合 - /// - public UnityEngine.Object[] AllAssets - { - get - { - if (IsValid == false) - return null; - return _provider.AllAssets; - } - } - - /// - /// 扩展的实例对象 - /// - public IAssetInstance AssetInstance - { - get - { - if (IsValid == false) - return null; - return _provider.AssetInstance; - } - } - /// /// 初始化的游戏对象(只限于请求的资源对象类型为GameObject) /// @@ -143,17 +67,6 @@ namespace YooAsset } } - /// - /// 释放资源句柄 - /// - public void Release() - { - if (IsValid == false) - return; - _provider.Release(); - _provider = null; - } - /// /// 等待异步执行完毕 /// @@ -163,28 +76,5 @@ namespace YooAsset return; _provider.WaitForAsyncComplete(); } - - #region 异步操作相关 - /// - /// 异步操作任务 - /// - public System.Threading.Tasks.Task Task - { - get { return _provider.Task; } - } - - // 协程相关 - bool IEnumerator.MoveNext() - { - return !IsDone; - } - void IEnumerator.Reset() - { - } - object IEnumerator.Current - { - get { return AssetObject; } - } - #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs new file mode 100644 index 0000000..1014400 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs @@ -0,0 +1,104 @@ +using System.Collections; + +namespace YooAsset +{ + public abstract class OperationHandleBase : IEnumerator + { + internal AssetProviderBase _provider { private set; get; } + + internal OperationHandleBase(AssetProviderBase provider) + { + _provider = provider; + } + internal abstract void InvokeCallback(); + + /// + /// 当前状态 + /// + public EOperationStatus Status + { + get + { + if (IsValid == false) + return EOperationStatus.None; + if (_provider.Status == AssetProviderBase.EStatus.Fail) + return EOperationStatus.Failed; + else if (_provider.Status == AssetProviderBase.EStatus.Success) + return EOperationStatus.Succeed; + else + return EOperationStatus.None; + } + } + + /// + /// 加载进度 + /// + public float Progress + { + get + { + if (IsValid == false) + return 0; + return _provider.Progress; + } + } + + /// + /// 是否加载完毕 + /// + public bool IsDone + { + get + { + if (IsValid == false) + return false; + return _provider.IsDone; + } + } + + /// + /// 句柄是否有效 + /// + public bool IsValid + { + get + { + return _provider != null && _provider.IsDestroyed == false; + } + } + + /// + /// 释放句柄 + /// + public void Release() + { + if (IsValid == false) + return; + _provider.ReleaseHandle(this); + _provider = null; + } + + #region 异步操作相关 + /// + /// 异步操作任务 + /// + public System.Threading.Tasks.Task Task + { + get { return _provider.Task; } + } + + // 协程相关 + bool IEnumerator.MoveNext() + { + return !IsDone; + } + void IEnumerator.Reset() + { + } + object IEnumerator.Current + { + get { return _provider; } + } + #endregion + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/IAssetProvider.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs.meta similarity index 83% rename from Assets/YooAsset/Runtime/AssetSystem/Provider/IAssetProvider.cs.meta rename to Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs.meta index 5de2355..b3d2d44 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/IAssetProvider.cs.meta +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 54f7b01043e68994189cc954e55813aa +guid: a9f5acda1a4584648af7b47e19a0d24f MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs new file mode 100644 index 0000000..c2092a2 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs @@ -0,0 +1,73 @@ + +namespace YooAsset +{ + public class SceneOperationHandle : OperationHandleBase + { + private System.Action _callback; + + internal SceneOperationHandle(AssetProviderBase provider) : base(provider) + { + } + internal override void InvokeCallback() + { + if (IsValid) + { + _callback?.Invoke(this); + } + } + + /// + /// 完成委托 + /// + public event System.Action Completed + { + add + { + if (IsValid == false) + throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid"); + if (_provider.IsDone) + value.Invoke(this); + else + _callback += value; + } + remove + { + if (IsValid == false) + throw new System.Exception($"{nameof(SceneOperationHandle)} is invalid"); + _callback -= value; + } + } + + /// + /// 场景对象 + /// + public UnityEngine.SceneManagement.Scene Scene + { + get + { + if (IsValid == false) + return new UnityEngine.SceneManagement.Scene(); + return _provider.Scene; + } + } + + /// + /// 激活场景 + /// + public bool ActivateScene() + { + if (IsValid == false) + return false; + + if (Scene.IsValid() && Scene.isLoaded) + { + return UnityEngine.SceneManagement.SceneManager.SetActiveScene(Scene); + } + else + { + YooLogger.Warning($"Scene is invalid or not loaded : {Scene.name}"); + return false; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetParam.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs.meta similarity index 83% rename from Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetParam.cs.meta rename to Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs.meta index aa37de7..382dc84 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Instance/IAssetParam.cs.meta +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a8995b1e1e511714b948f7fa6275028e +guid: c5d788ba2dd3c5243ae2e0be8e780825 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/AssetProviderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/AssetProviderBase.cs index 6d0809a..d0843e2 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/AssetProviderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/AssetProviderBase.cs @@ -1,7 +1,9 @@ - +using System.Collections; +using System.Collections.Generic; + namespace YooAsset { - internal abstract class AssetProviderBase : IAssetProvider + internal abstract class AssetProviderBase { public enum EStatus { @@ -13,19 +15,55 @@ namespace YooAsset Fail, } - protected bool IsWaitForAsyncComplete { private set; get; } = false; - + /// + /// 资源路径 + /// public string AssetPath { private set; get; } + + /// + /// 资源对象的名称 + /// public string AssetName { private set; get; } + + /// + /// 资源对象的类型 + /// public System.Type AssetType { private set; get; } + + /// + /// 获取的资源对象 + /// public UnityEngine.Object AssetObject { protected set; get; } + + /// + /// 获取的资源对象集合 + /// public UnityEngine.Object[] AllAssets { protected set; get; } - public IAssetInstance AssetInstance { protected set; get; } - public EStatus Status { protected set; get; } - public int RefCount { private set; get; } - public AssetOperationHandle Handle { private set; get; } - public System.Action Callback { set; get; } + + /// + /// 获取的场景对象 + /// + public UnityEngine.SceneManagement.Scene Scene { protected set; get; } + + + /// + /// 当前的加载状态 + /// + public EStatus Status { protected set; get; } = EStatus.None; + + /// + /// 引用计数 + /// + public int RefCount { private set; get; } = 0; + + /// + /// 是否已经销毁 + /// public bool IsDestroyed { private set; get; } = false; + + /// + /// 是否完毕(成功或失败) + /// public bool IsDone { get @@ -33,13 +71,10 @@ namespace YooAsset return Status == EStatus.Success || Status == EStatus.Fail; } } - public bool IsValid - { - get - { - return IsDestroyed == false; - } - } + + /// + /// 加载进度 + /// public virtual float Progress { get @@ -47,34 +82,69 @@ namespace YooAsset return 0; } } - + + + protected bool IsWaitForAsyncComplete { private set; get; } = false; + private readonly List _handles = new List(); + public AssetProviderBase(string assetPath, System.Type assetType) { AssetPath = assetPath; AssetName = System.IO.Path.GetFileName(assetPath); AssetType = assetType; - Status = EStatus.None; - Handle = new AssetOperationHandle(this); } + /// + /// 轮询更新方法 + /// public abstract void Update(); + + /// + /// 销毁资源对象 + /// public virtual void Destory() { IsDestroyed = true; } - public void Reference() + /// + /// 创建操作句柄 + /// + /// + public OperationHandleBase CreateHandle() { + // 引用计数增加 RefCount++; + + OperationHandleBase handle; + if (IsSceneProvider()) + handle = new SceneOperationHandle(this); + else + handle = new AssetOperationHandle(this); + + _handles.Add(handle); + return handle; } - public void Release() + + /// + /// 释放操作句柄 + /// + public void ReleaseHandle(OperationHandleBase handle) { if (RefCount <= 0) YooLogger.Warning("Asset provider reference count is already zero. There may be resource leaks !"); + if (_handles.Remove(handle) == false) + throw new System.Exception("Should never get here !"); + + // 引用计数减少 RefCount--; } + + /// + /// 是否可以销毁 + /// public bool CanDestroy() { if (IsDone == false) @@ -97,7 +167,7 @@ namespace YooAsset /// /// 等待异步执行完毕 /// - public virtual void WaitForAsyncComplete() + public void WaitForAsyncComplete() { IsWaitForAsyncComplete = true; @@ -114,7 +184,7 @@ namespace YooAsset /// /// 异步操作任务 /// - System.Threading.Tasks.Task IAssetProvider.Task + public System.Threading.Tasks.Task Task { get { @@ -141,7 +211,10 @@ namespace YooAsset } protected void InvokeCompletion() { - Callback?.Invoke(Handle); + foreach (var handle in _handles) + { + handle.InvokeCallback(); + } _waitHandle?.Set(); } } diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs index 5236f25..2c7240f 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSceneProvider.cs @@ -7,7 +7,8 @@ namespace YooAsset { internal sealed class BundledSceneProvider : BundledProvider { - private SceneInstanceParam _param; + private readonly LoadSceneMode _sceneMode; + private readonly bool _activateOnLoad; private AsyncOperation _asyncOp; public override float Progress { @@ -19,10 +20,11 @@ namespace YooAsset } } - public BundledSceneProvider(string scenePath, SceneInstanceParam param) + public BundledSceneProvider(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad) : base(scenePath, null) { - _param = param; + _sceneMode = sceneMode; + _activateOnLoad = activateOnLoad; } public override void Update() { @@ -56,7 +58,7 @@ namespace YooAsset // 2. 加载场景 if (Status == EStatus.Loading) { - _asyncOp = SceneManager.LoadSceneAsync(AssetName, _param.LoadMode); + _asyncOp = SceneManager.LoadSceneAsync(AssetName, _sceneMode); if (_asyncOp != null) { _asyncOp.allowSceneActivation = true; @@ -75,13 +77,11 @@ namespace YooAsset { if (_asyncOp.isDone) { - SceneInstance instance = new SceneInstance(_asyncOp); - instance.Scene = SceneManager.GetSceneByName(AssetName); - AssetInstance = instance; - if (_param.ActivateOnLoad) - instance.Activate(); + Scene = SceneManager.GetSceneByName(AssetName); + if (_activateOnLoad) + SceneManager.SetActiveScene(Scene); - Status = instance.Scene.IsValid() ? EStatus.Success : EStatus.Fail; + Status = Scene.IsValid() ? EStatus.Success : EStatus.Fail; InvokeCompletion(); } } @@ -91,19 +91,11 @@ namespace YooAsset base.Destory(); // 卸载附加场景(异步方式卸载) - if (_param.LoadMode == LoadSceneMode.Additive) + if (_sceneMode == LoadSceneMode.Additive) { - var instance = AssetInstance as SceneInstance; - if (instance != null && instance.Scene != null) - { - if (instance.Scene.IsValid() && instance.Scene.isLoaded) - SceneManager.UnloadSceneAsync(instance.Scene); - } + if (Scene.IsValid() && Scene.isLoaded) + SceneManager.UnloadSceneAsync(Scene); } } - public override void WaitForAsyncComplete() - { - throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}."); - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs index a92b1ef..57de498 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSceneProvider.cs @@ -5,7 +5,8 @@ namespace YooAsset { internal sealed class DatabaseSceneProvider : AssetProviderBase { - private SceneInstanceParam _param; + private readonly LoadSceneMode _sceneMode; + private readonly bool _activateOnLoad; private AsyncOperation _asyncOp; public override float Progress { @@ -17,10 +18,11 @@ namespace YooAsset } } - public DatabaseSceneProvider(string scenePath, SceneInstanceParam param) + public DatabaseSceneProvider(string scenePath, LoadSceneMode sceneMode, bool activateOnLoad) : base(scenePath, null) { - _param = param; + _sceneMode = sceneMode; + _activateOnLoad = activateOnLoad; } public override void Update() { @@ -37,7 +39,7 @@ namespace YooAsset if (Status == EStatus.Loading) { LoadSceneParameters loadSceneParameters = new LoadSceneParameters(); - loadSceneParameters.loadSceneMode = _param.LoadMode; + loadSceneParameters.loadSceneMode = _sceneMode; _asyncOp = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(AssetPath, loadSceneParameters); if (_asyncOp != null) { @@ -57,13 +59,11 @@ namespace YooAsset { if (_asyncOp.isDone) { - SceneInstance instance = new SceneInstance(_asyncOp); - instance.Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); - AssetInstance = instance; - if(_param.ActivateOnLoad) - instance.Activate(); + Scene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); + if (_activateOnLoad) + SceneManager.SetActiveScene(Scene); - Status = instance.Scene.IsValid() ? EStatus.Success : EStatus.Fail; + Status = Scene.IsValid() ? EStatus.Success : EStatus.Fail; InvokeCompletion(); } } @@ -75,20 +75,12 @@ namespace YooAsset base.Destory(); // 卸载附加场景(异步方式卸载) - if (_param.LoadMode == LoadSceneMode.Additive) + if (_sceneMode == LoadSceneMode.Additive) { - var instance = AssetInstance as SceneInstance; - if(instance != null && instance.Scene != null) - { - if (instance.Scene.IsValid() && instance.Scene.isLoaded) - SceneManager.UnloadSceneAsync(instance.Scene); - } + if (Scene.IsValid() && Scene.isLoaded) + SceneManager.UnloadSceneAsync(Scene); } #endif } - public override void WaitForAsyncComplete() - { - throw new System.Exception($"Unity scene is not support {nameof(WaitForAsyncComplete)}."); - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs index 068e27b..18996ce 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs @@ -40,7 +40,7 @@ namespace YooAsset else { Status = EStatus.Loading; - } + } // 注意:模拟异步加载效果提前返回 if (IsWaitForAsyncComplete == false) @@ -50,14 +50,21 @@ namespace YooAsset // 1. 加载资源对象 if (Status == EStatus.Loading) { - var findAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(AssetPath); - List result = new List(findAssets.Length); - foreach (var findObj in findAssets) + if (AssetType == null) { - if (findObj.GetType() == AssetType) - result.Add(findObj); + AllAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath); + } + else + { + UnityEngine.Object[] findAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath); + List result = new List(findAssets.Length); + foreach (var findAsset in findAssets) + { + if (findAsset.GetType() == AssetType) + result.Add(findAsset); + } + AllAssets = result.ToArray(); } - AllAssets = result.ToArray(); Status = EStatus.Checking; } @@ -66,7 +73,7 @@ namespace YooAsset { Status = AllAssets == null ? EStatus.Fail : EStatus.Success; if (Status == EStatus.Fail) - YooLogger.Warning($"Failed to load all asset object : {AssetPath}"); + YooLogger.Warning($"Failed to load sub assets : {AssetName}"); InvokeCompletion(); } #endif diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/IAssetProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/IAssetProvider.cs deleted file mode 100644 index ff6ce39..0000000 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/IAssetProvider.cs +++ /dev/null @@ -1,114 +0,0 @@ - -namespace YooAsset -{ - /// - /// 资源提供者 - /// - internal interface IAssetProvider - { - /// - /// 资源路径 - /// - string AssetPath { get; } - - /// - /// 资源对象的名称 - /// - string AssetName { get; } - - /// - /// 资源对象的类型 - /// - System.Type AssetType { get; } - - /// - /// 获取的资源对象 - /// - UnityEngine.Object AssetObject { get; } - - /// - /// 获取的资源对象集合 - /// - UnityEngine.Object[] AllAssets { get; } - - /// - /// 扩展的实例对象 - /// - IAssetInstance AssetInstance { get; } - - /// - /// 当前的加载状态 - /// - AssetProviderBase.EStatus Status { get; } - - /// - /// 引用计数 - /// - int RefCount { get; } - - /// - /// 资源操作句柄 - /// - AssetOperationHandle Handle { get; } - - /// - /// 用户请求的回调 - /// - System.Action Callback { set; get; } - - /// - /// 是否已经销毁 - /// - bool IsDestroyed { get; } - - /// - /// 是否完毕(成功或失败) - /// - bool IsDone { get; } - - /// - /// 是否有效(AssetFileLoader销毁会导致Provider无效) - /// - bool IsValid { get; } - - /// - /// 加载进度 - /// - float Progress { get; } - - /// - /// 轮询更新方法 - /// - void Update(); - - /// - /// 销毁资源对象 - /// - void Destory(); - - /// - /// 引用计数递加 - /// - void Reference(); - - /// - /// 引用计数递减 - /// - void Release(); - - /// - /// 是否可以销毁 - /// - bool CanDestroy(); - - /// - /// 等待异步执行完毕 - /// - void WaitForAsyncComplete(); - - /// - /// 异步操作任务 - /// - System.Threading.Tasks.Task Task { get; } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 44b340d..548abb6 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using UnityEngine.SceneManagement; namespace YooAsset { @@ -288,69 +289,103 @@ namespace YooAsset AssetSystem.GetDebugReport(report); } + #region 场景接口 + /// + /// 异步加载场景 + /// + /// 场景对象相对路径 + /// 场景加载模式 + /// 加载完毕时是否主动激活 + public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode mode, bool activateOnLoad) + { + string scenePath = ConvertLocationToAssetPath(location); + var handle = AssetSystem.LoadSceneAsync(scenePath, mode, activateOnLoad); + return handle; + } + #endregion #region 资源加载接口 /// /// 同步加载资源对象 /// + /// 资源类型 /// 资源对象相对路径 public static AssetOperationHandle LoadAssetSync(string location) where TObject : class { return LoadAssetInternal(location, typeof(TObject), true); } - public static AssetOperationHandle LoadAssetSync(System.Type type, string location) + + /// + /// 同步加载资源对象 + /// + /// 资源对象相对路径 + /// 资源类型 + public static AssetOperationHandle LoadAssetSync(string location, System.Type type) { return LoadAssetInternal(location, type, true); } /// - /// 同步加载子资源对象集合 + /// 同步加载子资源对象 /// + /// 资源类型 /// 资源对象相对路径 public static AssetOperationHandle LoadSubAssetsSync(string location) { return LoadSubAssetsInternal(location, typeof(TObject), true); } - public static AssetOperationHandle LoadSubAssetsSync(System.Type type, string location) + + /// + /// 同步加载子资源对象 + /// + /// 资源对象相对路径 + /// 子对象类型 + public static AssetOperationHandle LoadSubAssetsSync(string location, System.Type type) { return LoadSubAssetsInternal(location, type, true); } + /// - /// 异步加载场景 + /// 异步加载资源对象 /// - public static AssetOperationHandle LoadSceneAsync(string location, SceneInstanceParam instanceParam) + /// 资源类型 + /// 资源对象相对路径 + public static AssetOperationHandle LoadAssetAsync(string location) { - string scenePath = ConvertLocationToAssetPath(location); - var handle = AssetSystem.LoadSceneAsync(scenePath, instanceParam); - return handle; + return LoadAssetInternal(location, typeof(TObject), false); } /// /// 异步加载资源对象 /// /// 资源对象相对路径 - public static AssetOperationHandle LoadAssetAsync(string location) - { - return LoadAssetInternal(location, typeof(TObject), false); - } - public static AssetOperationHandle LoadAssetAsync(System.Type type, string location) + /// 资源类型 + public static AssetOperationHandle LoadAssetAsync(string location, System.Type type) { return LoadAssetInternal(location, type, false); } /// - /// 异步加载子资源对象集合 + /// 异步加载子资源对象 /// + /// 资源类型 /// 资源对象相对路径 public static AssetOperationHandle LoadSubAssetsAsync(string location) { return LoadSubAssetsInternal(location, typeof(TObject), false); } - public static AssetOperationHandle LoadSubAssetsAsync(System.Type type, string location) + + /// + /// 异步加载子资源对象 + /// + /// 资源对象相对路径 + /// 子对象类型 + public static AssetOperationHandle LoadSubAssetsAsync(string location, System.Type type) { return LoadSubAssetsInternal(location, type, false); } + private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete) {