From bc9a4e07e27f8253b7bacc51c0a581bd7e74491c Mon Sep 17 00:00:00 2001 From: hevinci Date: Fri, 27 Oct 2023 16:08:38 +0800 Subject: [PATCH] update operation system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 异步操作增加执行优先级 --- .../VisualViewers/DebuggerAssetListViewer.cs | 2 +- .../YooAsset/Runtime/InitializeParameters.cs | 6 - .../OperationSystem/AsyncOperationBase.cs | 27 +++- .../OperationSystem/OperationSystem.cs | 37 ++++-- .../ResourceManager/Handle/HandleBase.cs | 12 +- .../Loader/BundleLoaderBase.cs | 6 - .../Provider/BundledAllAssetsProvider.cs | 50 ++++---- .../Provider/BundledAssetProvider.cs | 48 ++++---- .../Provider/BundledRawFileProvider.cs | 26 ++-- .../Provider/BundledSceneProvider.cs | 54 ++++---- .../Provider/BundledSubAssetsProvider.cs | 48 ++++---- .../Provider/CompletedProvider.cs | 15 ++- .../Provider/DatabaseAllAssetsProvider.cs | 50 ++++---- .../Provider/DatabaseAssetProvider.cs | 50 ++++---- .../Provider/DatabaseRawFileProvider.cs | 33 ++--- .../Provider/DatabaseSceneProvider.cs | 49 ++++---- .../Provider/DatabaseSubAssetsProvider.cs | 50 ++++---- .../ResourceManager/Provider/ProviderBase.cs | 104 ++++------------ .../ResourceManager/ResourceManager.cs | 116 +++++------------- .../ResourcePackage/ResourcePackage.cs | 37 +++--- 20 files changed, 391 insertions(+), 429 deletions(-) diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerAssetListViewer.cs b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerAssetListViewer.cs index edc150b..b509fd2 100644 --- a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerAssetListViewer.cs +++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerAssetListViewer.cs @@ -224,7 +224,7 @@ namespace YooAsset.Editor // Status StyleColor textColor; - if (providerInfo.Status == ProviderBase.EStatus.Failed.ToString()) + if (providerInfo.Status == EOperationStatus.Failed.ToString()) textColor = new StyleColor(Color.yellow); else textColor = label1.style.color; diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index a13d556..fb2cbce 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -80,12 +80,6 @@ namespace YooAsset /// public bool AutoDestroyAssetProvider = false; - /// - /// 资源加载每帧处理的最大时间片段 - /// 注意:默认值为MaxValue - /// - public long LoadingMaxTimeSlice = long.MaxValue; - /// /// 启用断点续传参数 /// 说明:当文件的大小大于设置的字节数时启用断点续传下载器 diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index f0f8fee..b09522d 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -5,16 +5,24 @@ using System.Threading.Tasks; namespace YooAsset { - public abstract class AsyncOperationBase : IEnumerator + public abstract class AsyncOperationBase : IEnumerator, IComparable { // 用户请求的回调 private Action _callback; + // 是否已经完成 + internal bool IsFinish = false; + /// /// 所属包裹 /// public string PackageName { private set; get; } + /// + /// 优先级 + /// + public uint Priority { set; get; } = 0; + /// /// 状态 /// @@ -80,7 +88,7 @@ namespace YooAsset internal abstract void InternalOnUpdate(); internal virtual void InternalOnAbort() { } - internal void SetPackageName(string packageName) + internal void Init(string packageName) { PackageName = packageName; } @@ -91,8 +99,14 @@ namespace YooAsset } internal void SetFinish() { + IsFinish = true; + + // 进度百分百完成 Progress = 1f; - _callback?.Invoke(this); //注意:如果完成回调内发生异常,会导致Task无限期等待 + + //注意:如果完成回调内发生异常,会导致Task无限期等待 + _callback?.Invoke(this); + if (_taskCompletionSource != null) _taskCompletionSource.TrySetResult(null); } @@ -115,6 +129,13 @@ namespace YooAsset _callback = null; } + #region 排序接口实现 + public int CompareTo(AsyncOperationBase other) + { + return other.Priority.CompareTo(this.Priority); + } + #endregion + #region 异步编程相关 bool IEnumerator.MoveNext() { diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs index 3a3df1f..1638b9b 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs @@ -45,29 +45,50 @@ namespace YooAsset { _frameTime = _watch.ElapsedMilliseconds; - // 添加新的异步操作 + // 添加新增的异步操作 if (_newList.Count > 0) { + bool sorting = false; + foreach (var operation in _newList) + { + if (operation.Priority > 0) + { + sorting = true; + break; + } + } + _operations.AddRange(_newList); _newList.Clear(); + + // 重新排序优先级 + if (sorting) + _operations.Sort(); } - // 更新所有的异步操作 - for (int i = _operations.Count - 1; i >= 0; i--) + // 更新进行中的异步操作 + for (int i = 0; i < _operations.Count; i++) { if (IsBusy) break; var operation = _operations[i]; + if (operation.IsFinish) + continue; + if (operation.IsDone == false) operation.InternalOnUpdate(); if (operation.IsDone) - { - // 注意:如果业务端发生异常,保证异步操作提前移除。 - _operations.RemoveAt(i); operation.SetFinish(); - } + } + + // 移除已经完成的异步操作 + for (int i = _operations.Count - 1; i >= 0; i--) + { + var operation = _operations[i]; + if (operation.IsFinish) + _operations.RemoveAt(i); } } @@ -113,7 +134,7 @@ namespace YooAsset public static void StartOperation(string packageName, AsyncOperationBase operation) { _newList.Add(operation); - operation.SetPackageName(packageName); + operation.Init(packageName); operation.SetStart(); } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs index 1c1233c..f78b0d8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs @@ -45,15 +45,7 @@ namespace YooAsset if (IsValidWithWarning == false) return EOperationStatus.None; - var status = Provider.Status; - if (status == ProviderBase.EStatus.None) - return EOperationStatus.None; - else if (status == ProviderBase.EStatus.Succeed) - return EOperationStatus.Succeed; - else if (status == ProviderBase.EStatus.Failed) - return EOperationStatus.Failed; - else - return EOperationStatus.Processing; + return Provider.Status; } } @@ -66,7 +58,7 @@ namespace YooAsset { if (IsValidWithWarning == false) return string.Empty; - return Provider.LastError; + return Provider.Error; } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs index 5ba111b..35a1c6d 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleLoaderBase.cs @@ -110,12 +110,6 @@ namespace YooAsset /// public void TryDestroyProviders() { - // TODO 不用等待资源包加载完成 - /* - if (IsDone() == false) - return; - */ - // 获取移除列表 _removeList.Clear(); foreach (var provider in _providers) diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs index 6491bed..82bd6a1 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs @@ -7,24 +7,26 @@ namespace YooAsset internal sealed class BundledAllAssetsProvider : ProviderBase { private AssetBundleRequest _cacheRequest; - - public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + + public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() { DebugBeginRecording(); - + } + internal override void InternalOnUpdate() + { if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -39,17 +41,15 @@ namespace YooAsset if (DependBundles.IsSucceed() == false) { - Status = EStatus.Failed; - LastError = DependBundles.GetLastError(); - InvokeCompletion(); + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); return; } if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } @@ -59,11 +59,11 @@ namespace YooAsset return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { if (IsWaitForAsyncComplete || IsForceDestroyComplete) { @@ -79,11 +79,11 @@ namespace YooAsset else _cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(MainAssetInfo.AssetType); } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { if (_cacheRequest != null) { @@ -102,16 +102,20 @@ namespace YooAsset } } - Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed; - if (Status == EStatus.Failed) + if (AllAssetObjects == null) { + string error; if (MainAssetInfo.AssetType == null) - LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; else - LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(LastError); + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } - InvokeCompletion(); } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs index 25be93d..72dc1ab 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs @@ -8,23 +8,25 @@ namespace YooAsset { private AssetBundleRequest _cacheRequest; - public BundledAssetProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public BundledAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() { DebugBeginRecording(); - + } + internal override void InternalOnUpdate() + { if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -39,17 +41,15 @@ namespace YooAsset if (DependBundles.IsSucceed() == false) { - Status = EStatus.Failed; - LastError = DependBundles.GetLastError(); - InvokeCompletion(); + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); return; } if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } @@ -59,11 +59,11 @@ namespace YooAsset return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { if (IsWaitForAsyncComplete || IsForceDestroyComplete) { @@ -79,11 +79,11 @@ namespace YooAsset else _cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { if (_cacheRequest != null) { @@ -102,16 +102,20 @@ namespace YooAsset } } - Status = AssetObject == null ? EStatus.Failed : EStatus.Succeed; - if (Status == EStatus.Failed) + if (AssetObject == null) { + string error; if (MainAssetInfo.AssetType == null) - LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; else - LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(LastError); + error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } - InvokeCompletion(); } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs index 90d1f35..dfd0042 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs @@ -3,23 +3,25 @@ namespace YooAsset { internal class BundledRawFileProvider : ProviderBase { - public BundledRawFileProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public BundledRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() { DebugBeginRecording(); - + } + internal override void InternalOnUpdate() + { if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -31,21 +33,19 @@ namespace YooAsset if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 2. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { RawFilePath = OwnerBundle.FileLoadPath; - Status = EStatus.Succeed; - InvokeCompletion(); + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs index 4c2e67c..0e313dc 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs @@ -12,26 +12,28 @@ namespace YooAsset private readonly bool _suspendLoad; private AsyncOperation _asyncOperation; - public BundledSceneProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, providerPriority, assetInfo) + public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) { SceneMode = sceneMode; SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); _suspendLoad = suspendLoad; } - public override void Update() + internal override void InternalOnStart() { DebugBeginRecording(); - + } + internal override void InternalOnUpdate() + { if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (DependBundles.IsDone() == false) return; @@ -40,57 +42,57 @@ namespace YooAsset if (DependBundles.IsSucceed() == false) { - Status = EStatus.Failed; - LastError = DependBundles.GetLastError(); - InvokeCompletion(); + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); return; } if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载场景 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { // 注意:如果场景不存在则返回NULL _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode); if (_asyncOperation != null) { _asyncOperation.allowSceneActivation = !_suspendLoad; - _asyncOperation.priority = (int)ProviderPriority; + _asyncOperation.priority = 100; SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); - Status = EStatus.Checking; + _steps = ESteps.Checking; } else { - Status = EStatus.Failed; - LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"Failed to load scene : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); } } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { Progress = _asyncOperation.progress; if (_asyncOperation.isDone) { - Status = SceneObject.IsValid() ? EStatus.Succeed : EStatus.Failed; - if (Status == EStatus.Failed) + if (SceneObject.IsValid()) { - LastError = $"The load scene is invalid : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + else + { + string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); } - InvokeCompletion(); } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs index 496f8fa..c33612f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs @@ -8,23 +8,25 @@ namespace YooAsset { private AssetBundleRequest _cacheRequest; - public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() { DebugBeginRecording(); - + } + internal override void InternalOnUpdate() + { if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -39,17 +41,15 @@ namespace YooAsset if (DependBundles.IsSucceed() == false) { - Status = EStatus.Failed; - LastError = DependBundles.GetLastError(); - InvokeCompletion(); + string error = DependBundles.GetLastError(); + InvokeCompletion(error, EOperationStatus.Failed); return; } if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } @@ -59,11 +59,11 @@ namespace YooAsset return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { if (IsWaitForAsyncComplete || IsForceDestroyComplete) { @@ -79,11 +79,11 @@ namespace YooAsset else _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { if (_cacheRequest != null) { @@ -102,16 +102,20 @@ namespace YooAsset } } - Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed; - if (Status == EStatus.Failed) + if (AllAssetObjects == null) { + string error; if (MainAssetInfo.AssetType == null) - LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; else - LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; - YooLogger.Error(LastError); + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } - InvokeCompletion(); } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs index e674215..fa495ef 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs @@ -3,19 +3,22 @@ namespace YooAsset { internal sealed class CompletedProvider : ProviderBase { - public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, 0, assetInfo) + public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo) { } - public override void Update() + + internal override void InternalOnStart() { } + internal override void InternalOnUpdate() + { + } + public void SetCompleted(string error) { - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.Failed; - LastError = error; - InvokeCompletion(); + InvokeCompletion(error, EOperationStatus.Failed); } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs index 4dd1c48..174d665 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs @@ -6,29 +6,32 @@ namespace YooAsset { internal sealed class DatabaseAllAssetsProvider : ProviderBase { - public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() { #if UNITY_EDITOR if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { // 检测资源文件是否存在 string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); if (string.IsNullOrEmpty(guid)) { - Status = EStatus.Failed; - LastError = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; // 注意:模拟异步加载效果提前返回 if (IsWaitForAsyncComplete == false) @@ -36,7 +39,7 @@ namespace YooAsset } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -48,17 +51,16 @@ namespace YooAsset if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { if (MainAssetInfo.AssetType == null) { @@ -82,22 +84,26 @@ namespace YooAsset } AllAssetObjects = result.ToArray(); } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { - Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed; - if (Status == EStatus.Failed) + if (AllAssetObjects == null) { + string error; if (MainAssetInfo.AssetType == null) - LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null"; + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null"; else - LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; - YooLogger.Error(LastError); + error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } - InvokeCompletion(); } #endif } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs index 89b526c..96f790f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs @@ -6,29 +6,32 @@ namespace YooAsset { internal sealed class DatabaseAssetProvider : ProviderBase { - public DatabaseAssetProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public DatabaseAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() { #if UNITY_EDITOR if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { // 检测资源文件是否存在 string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); if (string.IsNullOrEmpty(guid)) { - Status = EStatus.Failed; - LastError = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; // 注意:模拟异步加载效果提前返回 if (IsWaitForAsyncComplete == false) @@ -36,7 +39,7 @@ namespace YooAsset } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -48,38 +51,41 @@ namespace YooAsset if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { if (MainAssetInfo.AssetType == null) AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath); else AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType); - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { - Status = AssetObject == null ? EStatus.Failed : EStatus.Succeed; - if (Status == EStatus.Failed) + if (AssetObject == null) { + string error; if (MainAssetInfo.AssetType == null) - LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null"; + error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null"; else - LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; - YooLogger.Error(LastError); + error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } - InvokeCompletion(); } #endif } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs index 3d9de80..a674524 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs @@ -3,29 +3,32 @@ namespace YooAsset { internal class DatabaseRawFileProvider : ProviderBase { - public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() { #if UNITY_EDITOR if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { // 检测资源文件是否存在 string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); if (string.IsNullOrEmpty(guid)) { - Status = EStatus.Failed; - LastError = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; // 注意:模拟异步加载效果提前返回 if (IsWaitForAsyncComplete == false) @@ -33,7 +36,7 @@ namespace YooAsset } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -45,21 +48,19 @@ namespace YooAsset if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 2. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { RawFilePath = MainAssetInfo.AssetPath; - Status = EStatus.Succeed; - InvokeCompletion(); + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } #endif } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs index 2619f94..49e5d66 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs @@ -12,25 +12,29 @@ namespace YooAsset private readonly bool _suspendLoad; private AsyncOperation _asyncOperation; - public DatabaseSceneProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, providerPriority, assetInfo) + public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo) { SceneMode = sceneMode; SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath); _suspendLoad = suspendLoad; } - public override void Update() + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() { #if UNITY_EDITOR if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -42,17 +46,16 @@ namespace YooAsset if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { LoadSceneParameters loadSceneParameters = new LoadSceneParameters(); loadSceneParameters.loadSceneMode = SceneMode; @@ -60,32 +63,34 @@ namespace YooAsset if (_asyncOperation != null) { _asyncOperation.allowSceneActivation = !_suspendLoad; - _asyncOperation.priority = (int)ProviderPriority; + _asyncOperation.priority = 100; SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1); - Status = EStatus.Checking; + _steps = ESteps.Checking; } else { - Status = EStatus.Failed; - LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"Failed to load scene : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); } } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { Progress = _asyncOperation.progress; if (_asyncOperation.isDone) { - Status = SceneObject.IsValid() ? EStatus.Succeed : EStatus.Failed; - if (Status == EStatus.Failed) + if (SceneObject.IsValid()) { - LastError = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); + InvokeCompletion(string.Empty, EOperationStatus.Succeed); + } + else + { + string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); } - InvokeCompletion(); } } #endif diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs index 6c1e2a0..5f97102 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs @@ -6,29 +6,32 @@ namespace YooAsset { internal sealed class DatabaseSubAssetsProvider : ProviderBase { - public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo) + public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo) { } - public override void Update() + internal override void InternalOnStart() + { + DebugBeginRecording(); + } + internal override void InternalOnUpdate() { #if UNITY_EDITOR if (IsDone) return; - if (Status == EStatus.None) + if (_steps == ESteps.None) { // 检测资源文件是否存在 string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath); if (string.IsNullOrEmpty(guid)) { - Status = EStatus.Failed; - LastError = $"Not found asset : {MainAssetInfo.AssetPath}"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"Not found asset : {MainAssetInfo.AssetPath}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.CheckBundle; + _steps = ESteps.CheckBundle; // 注意:模拟异步加载效果提前返回 if (IsWaitForAsyncComplete == false) @@ -36,7 +39,7 @@ namespace YooAsset } // 1. 检测资源包 - if (Status == EStatus.CheckBundle) + if (_steps == ESteps.CheckBundle) { if (IsWaitForAsyncComplete) { @@ -48,17 +51,16 @@ namespace YooAsset if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed) { - Status = EStatus.Failed; - LastError = OwnerBundle.LastError; - InvokeCompletion(); + string error = OwnerBundle.LastError; + InvokeCompletion(error, EOperationStatus.Failed); return; } - Status = EStatus.Loading; + _steps = ESteps.Loading; } // 2. 加载资源对象 - if (Status == EStatus.Loading) + if (_steps == ESteps.Loading) { if (MainAssetInfo.AssetType == null) { @@ -75,22 +77,26 @@ namespace YooAsset } AllAssetObjects = result.ToArray(); } - Status = EStatus.Checking; + _steps = ESteps.Checking; } // 3. 检测加载结果 - if (Status == EStatus.Checking) + if (_steps == ESteps.Checking) { - Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed; - if (Status == EStatus.Failed) + if (AllAssetObjects == null) { + string error; if (MainAssetInfo.AssetType == null) - LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null"; + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null"; else - LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; - YooLogger.Error(LastError); + error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}"; + YooLogger.Error(error); + InvokeCompletion(error, EOperationStatus.Failed); + } + else + { + InvokeCompletion(string.Empty, EOperationStatus.Succeed); } - InvokeCompletion(); } #endif } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs index 0364ad3..0421d70 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs @@ -6,16 +6,15 @@ using System; namespace YooAsset { - internal abstract class ProviderBase : IComparable + internal abstract class ProviderBase : AsyncOperationBase { - public enum EStatus + protected enum ESteps { None = 0, CheckBundle, Loading, Checking, - Succeed, - Failed, + Done, } /// @@ -23,11 +22,6 @@ namespace YooAsset /// public string ProviderGUID { private set; get; } - /// - /// 资源加载的优先级 - /// - public uint ProviderPriority { private set; get; } - /// /// 所属资源系统 /// @@ -63,22 +57,6 @@ namespace YooAsset /// public string RawFilePath { protected set; get; } - - /// - /// 当前的加载状态 - /// - public EStatus Status { protected set; get; } = EStatus.None; - - /// - /// 最近的错误信息 - /// - public string LastError { protected set; get; } = string.Empty; - - /// - /// 加载进度 - /// - public float Progress { protected set; get; } = 0f; - /// /// 引用计数 /// @@ -89,18 +67,8 @@ namespace YooAsset /// public bool IsDestroyed { private set; get; } = false; - /// - /// 是否完毕(成功或失败) - /// - public bool IsDone - { - get - { - return Status == EStatus.Succeed || Status == EStatus.Failed; - } - } - + protected ESteps _steps = ESteps.None; protected BundleLoaderBase OwnerBundle { private set; get; } protected DependAssetBundles DependBundles { private set; get; } protected bool IsWaitForAsyncComplete { private set; get; } = false; @@ -108,11 +76,10 @@ namespace YooAsset private readonly List _handles = new List(); - public ProviderBase(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) + public ProviderBase(ResourceManager manager, string providerGUID, AssetInfo assetInfo) { ResourceMgr = manager; ProviderGUID = providerGUID; - ProviderPriority = providerPriority; MainAssetInfo = assetInfo; // 创建资源包加载器 @@ -128,11 +95,6 @@ namespace YooAsset } } - /// - /// 轮询更新方法 - /// - public abstract void Update(); - /// /// 销毁资源提供者 /// @@ -140,6 +102,13 @@ namespace YooAsset { IsDestroyed = true; + // 检测是否为正常销毁 + if (IsDone == false) + { + Error = "User abort !"; + Status = EOperationStatus.Failed; + } + // 释放资源包加载器 if (OwnerBundle != null) { @@ -159,7 +128,7 @@ namespace YooAsset public bool CanDestroy() { // 注意:在进行资源加载过程时不可以销毁 - if (Status == EStatus.Loading || Status == EStatus.Checking) + if (_steps == ESteps.Loading || _steps == ESteps.Checking) return false; return RefCount <= 0; @@ -226,7 +195,7 @@ namespace YooAsset IsWaitForAsyncComplete = true; // 注意:主动轮询更新完成同步加载 - Update(); + InternalOnUpdate(); // 验证结果 if (IsDone == false) @@ -244,7 +213,7 @@ namespace YooAsset // 注意:主动轮询更新完成同步加载 // 说明:如果资源包未准备完毕也可以放心销毁。 - Update(); + InternalOnUpdate(); } /// @@ -255,44 +224,21 @@ namespace YooAsset if (OwnerBundle.IsDestroyed) throw new System.Exception("Should never get here !"); - Status = EStatus.Failed; - LastError = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !"; - YooLogger.Error(LastError); - InvokeCompletion(); + string error = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !"; + YooLogger.Error(error); + InvokeCompletion(Error, EOperationStatus.Failed); } /// - /// 异步操作任务 + /// 结束流程 /// - public Task Task - { - get - { - if (_taskCompletionSource == null) - { - _taskCompletionSource = new TaskCompletionSource(); - if (IsDone) - _taskCompletionSource.SetResult(null); - } - return _taskCompletionSource.Task; - } - } - - #region 排序接口实现 - public int CompareTo(ProviderBase other) - { - return other.ProviderPriority.CompareTo(this.ProviderPriority); - } - #endregion - - #region 异步编程相关 - private TaskCompletionSource _taskCompletionSource; - protected void InvokeCompletion() + protected void InvokeCompletion(string error, EOperationStatus status) { DebugEndRecording(); - // 进度百分百完成 - Progress = 1f; + _steps = ESteps.Done; + Error = error; + Status = status; // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。 // 注意:回调方法如果发生异常,会阻断列表里的后续回调方法! @@ -304,11 +250,7 @@ namespace YooAsset hande.InvokeCallback(); } } - - if (_taskCompletionSource != null) - _taskCompletionSource.TrySetResult(null); } - #endregion #region 调试信息相关 /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs index 6d72df3..adaef84 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs @@ -9,32 +9,17 @@ namespace YooAsset { internal class ResourceManager { - private readonly Dictionary _loaderDic = new Dictionary(5000); - private readonly List _loaderList = new List(5000); - - private readonly Dictionary _providerDic = new Dictionary(5000); - private readonly List _providerList = new List(5000); - + // 全局场景句柄集合 private readonly static Dictionary _sceneHandles = new Dictionary(100); private static long _sceneCreateCount = 0; + private readonly Dictionary _providerDic = new Dictionary(5000); + private readonly Dictionary _loaderDic = new Dictionary(5000); + private readonly List _loaderList = new List(5000); private bool _simulationOnEditor; private bool _autoDestroyAssetProvider; - private long _loadingMaxTimeSlice; private IBundleQuery _bundleQuery; - private bool _isUnloadSafe = true; - - // 计时器相关 - private Stopwatch _watch; - private long _frameTime; - private bool IsBusy - { - get - { - return _watch.ElapsedMilliseconds - _frameTime >= _loadingMaxTimeSlice; - } - } /// /// 所属包裹 @@ -50,13 +35,11 @@ namespace YooAsset /// /// 初始化 /// - public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, long loadingMaxTimeSlice, IBundleQuery bundleServices) + public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, IBundleQuery bundleServices) { _simulationOnEditor = simulationOnEditor; _autoDestroyAssetProvider = autoDestroyAssetProvider; - _loadingMaxTimeSlice = loadingMaxTimeSlice; _bundleQuery = bundleServices; - _watch = Stopwatch.StartNew(); } /// @@ -64,9 +47,6 @@ namespace YooAsset /// public void Update() { - _frameTime = _watch.ElapsedMilliseconds; - - // 更新加载器 foreach (var loader in _loaderList) { loader.Update(); @@ -74,17 +54,6 @@ namespace YooAsset if (_autoDestroyAssetProvider) loader.TryDestroyProviders(); } - - // 更新资源提供者 - // 注意:循环更新的时候,可能会扩展列表 - _isUnloadSafe = false; - for (int i = 0; i < _providerList.Count; i++) - { - if (IsBusy) - break; - _providerList[i].Update(); - } - _isUnloadSafe = true; } /// @@ -92,12 +61,6 @@ namespace YooAsset /// public void UnloadUnusedAssets() { - if (_isUnloadSafe == false) - { - YooLogger.Warning("Can not unload unused assets when processing resource loading !"); - return; - } - // 注意:资源包之间可能存在多层深层嵌套,需要多次循环释放。 int loopCount = 10; for (int i = 0; i < loopCount; i++) @@ -191,13 +154,13 @@ namespace YooAsset } // 释放所有资源句柄 - foreach (var provider in _providerList) + foreach (var provider in _providerDic.Values) { provider.ReleaseAllHandles(); } // 强制销毁资源提供者 - foreach (var provider in _providerList) + foreach (var provider in _providerDic.Values) { provider.ForceDestroyComplete(); provider.Destroy(); @@ -211,7 +174,6 @@ namespace YooAsset } // 清空数据 - _providerList.Clear(); _providerDic.Clear(); _loaderList.Clear(); _loaderDic.Clear(); @@ -248,18 +210,15 @@ namespace YooAsset ProviderBase provider; { if (_simulationOnEditor) - provider = new DatabaseSceneProvider(this, providerGUID, priority, assetInfo, sceneMode, suspendLoad); + provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad); else - provider = new BundledSceneProvider(this, providerGUID, priority, assetInfo, sceneMode, suspendLoad); + provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad); provider.InitSpawnDebugInfo(); - _providerList.Add(provider); _providerDic.Add(providerGUID, provider); - if (priority > 0) - { - _providerList.Sort(); - } + OperationSystem.StartOperation(PackageName, provider); } + provider.Priority = priority; var handle = provider.CreateHandle(); handle.PackageName = PackageName; _sceneHandles.Add(providerGUID, handle); @@ -284,17 +243,15 @@ namespace YooAsset if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseAssetProvider(this, providerGUID, priority, assetInfo); + provider = new DatabaseAssetProvider(this, providerGUID, assetInfo); else - provider = new BundledAssetProvider(this, providerGUID, priority, assetInfo); + provider = new BundledAssetProvider(this, providerGUID, assetInfo); provider.InitSpawnDebugInfo(); - _providerList.Add(provider); _providerDic.Add(providerGUID, provider); - if (priority > 0) - { - _providerList.Sort(); - } + OperationSystem.StartOperation(PackageName, provider); } + + provider.Priority = priority; return provider.CreateHandle(); } @@ -316,17 +273,15 @@ namespace YooAsset if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseSubAssetsProvider(this, providerGUID, priority, assetInfo); + provider = new DatabaseSubAssetsProvider(this, providerGUID, assetInfo); else - provider = new BundledSubAssetsProvider(this, providerGUID, priority, assetInfo); + provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo); provider.InitSpawnDebugInfo(); - _providerList.Add(provider); _providerDic.Add(providerGUID, provider); - if (priority > 0) - { - _providerList.Sort(); - } + OperationSystem.StartOperation(PackageName, provider); } + + provider.Priority = priority; return provider.CreateHandle(); } @@ -348,17 +303,15 @@ namespace YooAsset if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseAllAssetsProvider(this, providerGUID, priority, assetInfo); + provider = new DatabaseAllAssetsProvider(this, providerGUID, assetInfo); else - provider = new BundledAllAssetsProvider(this, providerGUID, priority, assetInfo); + provider = new BundledAllAssetsProvider(this, providerGUID, assetInfo); provider.InitSpawnDebugInfo(); - _providerList.Add(provider); _providerDic.Add(providerGUID, provider); - if (priority > 0) - { - _providerList.Sort(); - } + OperationSystem.StartOperation(PackageName, provider); } + + provider.Priority = priority; return provider.CreateHandle(); } @@ -380,17 +333,15 @@ namespace YooAsset if (provider == null) { if (_simulationOnEditor) - provider = new DatabaseRawFileProvider(this, providerGUID, priority, assetInfo); + provider = new DatabaseRawFileProvider(this, providerGUID, assetInfo); else - provider = new BundledRawFileProvider(this, providerGUID, priority, assetInfo); + provider = new BundledRawFileProvider(this, providerGUID, assetInfo); provider.InitSpawnDebugInfo(); - _providerList.Add(provider); _providerDic.Add(providerGUID, provider); - if (priority > 0) - { - _providerList.Sort(); - } + OperationSystem.StartOperation(PackageName, provider); } + + provider.Priority = priority; return provider.CreateHandle(); } @@ -463,7 +414,6 @@ namespace YooAsset { foreach (var provider in removeList) { - _providerList.Remove(provider); _providerDic.Remove(provider.ProviderGUID); } } @@ -522,8 +472,8 @@ namespace YooAsset #region 调试信息 internal List GetDebugReportInfos() { - List result = new List(_providerList.Count); - foreach (var provider in _providerList) + List result = new List(_providerDic.Count); + foreach (var provider in _providerDic.Values) { DebugProviderInfo providerInfo = new DebugProviderInfo(); providerInfo.AssetPath = provider.MainAssetInfo.AssetPath; diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs index 4da46b5..4384b6e 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs @@ -146,7 +146,7 @@ namespace YooAsset var editorSimulateModeImpl = new EditorSimulateModeImpl(PackageName); _bundleQuery = editorSimulateModeImpl; _playModeImpl = editorSimulateModeImpl; - _resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery); + _resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, _bundleQuery); var initializeParameters = parameters as EditorSimulateModeParameters; initializeOperation = editorSimulateModeImpl.InitializeAsync(assist, initializeParameters.SimulateManifestFilePath); @@ -156,7 +156,7 @@ namespace YooAsset var offlinePlayModeImpl = new OfflinePlayModeImpl(PackageName); _bundleQuery = offlinePlayModeImpl; _playModeImpl = offlinePlayModeImpl; - _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery); + _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); var initializeParameters = parameters as OfflinePlayModeParameters; initializeOperation = offlinePlayModeImpl.InitializeAsync(assist); @@ -166,7 +166,7 @@ namespace YooAsset var hostPlayModeImpl = new HostPlayModeImpl(PackageName); _bundleQuery = hostPlayModeImpl; _playModeImpl = hostPlayModeImpl; - _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery); + _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); var initializeParameters = parameters as HostPlayModeParameters; initializeOperation = hostPlayModeImpl.InitializeAsync(assist, @@ -179,7 +179,7 @@ namespace YooAsset var webPlayModeImpl = new WebPlayModeImpl(PackageName); _bundleQuery = webPlayModeImpl; _playModeImpl = webPlayModeImpl; - _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery); + _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery); var initializeParameters = parameters as WebPlayModeParameters; initializeOperation = webPlayModeImpl.InitializeAsync(assist, @@ -266,13 +266,6 @@ namespace YooAsset } #endif } - - // 检测参数范围 - if (parameters.LoadingMaxTimeSlice < 10) - { - parameters.LoadingMaxTimeSlice = 10; - YooLogger.Warning($"{nameof(parameters.LoadingMaxTimeSlice)} minimum value is 10 milliseconds."); - } } private void InitializeOperation_Completed(AsyncOperationBase op) { @@ -566,6 +559,7 @@ namespace YooAsset /// 异步加载原生文件 /// /// 资源信息 + /// 加载的优先级 public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0) { DebugCheckInitialize(); @@ -576,6 +570,7 @@ namespace YooAsset /// 异步加载原生文件 /// /// 资源的定位地址 + /// 加载的优先级 public RawFileHandle LoadRawFileAsync(string location, uint priority = 0) { DebugCheckInitialize(); @@ -601,8 +596,8 @@ namespace YooAsset /// 场景的定位地址 /// 场景加载模式 /// 场景加载到90%自动挂起 - /// 优先级 - public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100) + /// 加载的优先级 + public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) { DebugCheckInitialize(); AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); @@ -616,8 +611,8 @@ namespace YooAsset /// 场景的资源信息 /// 场景加载模式 /// 场景加载到90%自动挂起 - /// 优先级 - public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100) + /// 加载的优先级 + public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) { DebugCheckInitialize(); var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); @@ -677,6 +672,7 @@ namespace YooAsset /// 异步加载资源对象 /// /// 资源信息 + /// 加载的优先级 public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0) { DebugCheckInitialize(); @@ -688,6 +684,7 @@ namespace YooAsset /// /// 资源类型 /// 资源的定位地址 + /// 加载的优先级 public AssetHandle LoadAssetAsync(string location, uint priority = 0) where TObject : UnityEngine.Object { DebugCheckInitialize(); @@ -700,6 +697,7 @@ namespace YooAsset /// /// 资源的定位地址 /// 资源类型 + /// 加载的优先级 public AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0) { DebugCheckInitialize(); @@ -711,6 +709,7 @@ namespace YooAsset /// 异步加载资源对象 /// /// 资源的定位地址 + /// 加载的优先级 public AssetHandle LoadAssetAsync(string location, uint priority = 0) { DebugCheckInitialize(); @@ -783,6 +782,7 @@ namespace YooAsset /// 异步加载子资源对象 /// /// 资源信息 + /// 加载的优先级 public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0) { DebugCheckInitialize(); @@ -794,6 +794,7 @@ namespace YooAsset /// /// 资源类型 /// 资源的定位地址 + /// 加载的优先级 public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object { DebugCheckInitialize(); @@ -806,6 +807,7 @@ namespace YooAsset /// /// 资源的定位地址 /// 子对象类型 + /// 加载的优先级 public SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0) { DebugCheckInitialize(); @@ -817,6 +819,7 @@ namespace YooAsset /// 异步加载子资源对象 /// /// 资源的定位地址 + /// 加载的优先级 public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0) { DebugCheckInitialize(); @@ -889,6 +892,7 @@ namespace YooAsset /// 异步加载资源包内所有资源对象 /// /// 资源信息 + /// 加载的优先级 public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0) { DebugCheckInitialize(); @@ -900,6 +904,7 @@ namespace YooAsset /// /// 资源类型 /// 资源的定位地址 + /// 加载的优先级 public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) where TObject : UnityEngine.Object { DebugCheckInitialize(); @@ -912,6 +917,7 @@ namespace YooAsset /// /// 资源的定位地址 /// 子对象类型 + /// 加载的优先级 public AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0) { DebugCheckInitialize(); @@ -923,6 +929,7 @@ namespace YooAsset /// 异步加载资源包内所有资源对象 /// /// 资源的定位地址 + /// 加载的优先级 public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0) { DebugCheckInitialize();