From a37663a8c2d52cef7fd6f307917bc398d3f0b2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Thu, 8 Jan 2026 19:19:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=E9=87=8D=E6=9E=84=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=93=8D=E4=BD=9C=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClearAllCacheBundleFilesOperation.cs | 2 +- ...earCacheBundleFilesByLocationsOperaiton.cs | 2 +- .../ClearCacheBundleFilesByTagsOperaiton.cs | 2 +- .../ClearUnusedCacheBundleFilesOperation.cs | 2 +- .../internal/SearchCacheFilesOperation.cs | 2 +- .../internal/VerifyCacheFilesOperation.cs | 2 +- .../OperationSystem/AsyncOperationBase.cs | 106 ++++++++++-------- .../OperationSystem/GameAsyncOperation.cs | 34 ------ .../Operation/UnloadUnusedAssetsOperation.cs | 7 +- .../Internal/DeserializeManifestOperation.cs | 14 +-- 10 files changed, 74 insertions(+), 99 deletions(-) diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearAllCacheBundleFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearAllCacheBundleFilesOperation.cs index 5b3db2ac..b5258fd9 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearAllCacheBundleFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearAllCacheBundleFilesOperation.cs @@ -47,7 +47,7 @@ namespace YooAsset string bundleGUID = _allBundleGUIDs[i]; _fileSystem.DeleteCacheBundleFile(bundleGUID); _allBundleGUIDs.RemoveAt(i); - if (OperationSystem.IsBusy) + if (IsBusy) break; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByLocationsOperaiton.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByLocationsOperaiton.cs index 69316ac5..79b1397a 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByLocationsOperaiton.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByLocationsOperaiton.cs @@ -101,7 +101,7 @@ namespace YooAsset string bundleGUID = _clearBundleGUIDs[i]; _fileSystem.DeleteCacheBundleFile(bundleGUID); _clearBundleGUIDs.RemoveAt(i); - if (OperationSystem.IsBusy) + if (IsBusy) break; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByTagsOperaiton.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByTagsOperaiton.cs index eef4797a..247bc01c 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByTagsOperaiton.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearCacheBundleFilesByTagsOperaiton.cs @@ -99,7 +99,7 @@ namespace YooAsset string bundleGUID = _clearBundleGUIDs[i]; _fileSystem.DeleteCacheBundleFile(bundleGUID); _clearBundleGUIDs.RemoveAt(i); - if (OperationSystem.IsBusy) + if (IsBusy) break; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearUnusedCacheBundleFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearUnusedCacheBundleFilesOperation.cs index 63841e14..fd0bf90e 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearUnusedCacheBundleFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/ClearUnusedCacheBundleFilesOperation.cs @@ -64,7 +64,7 @@ namespace YooAsset string bundleGUID = _unusedBundleGUIDs[i]; _fileSystem.DeleteCacheBundleFile(bundleGUID); _unusedBundleGUIDs.RemoveAt(i); - if (OperationSystem.IsBusy) + if (IsBusy) break; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs index 04d1964d..a297e886 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs @@ -102,7 +102,7 @@ namespace YooAsset Result.Add(element); } - if (OperationSystem.IsBusy) + if (IsBusy) break; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs index 5b36fe1a..c6581091 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs @@ -90,7 +90,7 @@ namespace YooAsset for (int i = _waitingList.Count - 1; i >= 0; i--) { - if (OperationSystem.IsBusy) + if (IsBusy) break; if (_verifyingList.Count >= _verifyMaxNum) diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index 0d102753..016eaa5b 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -8,14 +8,10 @@ namespace YooAsset { public abstract class AsyncOperationBase : IEnumerator, IComparable { + private List _childs; private Action _callback; private int _whileFrame = 1000; - /// - /// 所有子任务 - /// - internal readonly List Childs = new List(10); - /// /// 等待异步执行完成 /// @@ -26,6 +22,19 @@ namespace YooAsset /// internal bool IsFinish { private set; get; } = false; + /// + /// 异步系统是否繁忙 + /// + internal bool IsBusy + { + get + { + if (IsWaitForAsyncComplete) + return false; + return OperationSystem.IsBusy; + } + } + /// /// 任务优先级 /// @@ -125,12 +134,15 @@ namespace YooAsset /// internal void AddChildOperation(AsyncOperationBase child) { + if (_childs == null) + _childs = new List(10); + #if UNITY_EDITOR - if (Childs.Contains(child)) + if (_childs.Contains(child)) throw new YooInternalException($"The child node {child.GetType().Name} already exists !"); #endif - Childs.Add(child); + _childs.Add(child); } /// @@ -138,12 +150,15 @@ namespace YooAsset /// internal void RemoveChildOperation(AsyncOperationBase child) { + if (_childs == null) + return; + #if UNITY_EDITOR - if (Childs.Contains(child) == false) + if (_childs.Contains(child) == false) throw new YooInternalException($"The child node {child.GetType().Name} not exists !"); #endif - Childs.Remove(child); + _childs.Remove(child); } /// @@ -187,25 +202,7 @@ namespace YooAsset if (IsDone && IsFinish == false) { - IsFinish = true; - Progress = 1f; - - // 结束记录 - DebugEndRecording(); - - try - { - _callback?.Invoke(this); - } - catch (Exception ex) - { - YooLogger.Error($"Exception in completion callback: {ex}"); - } - finally - { - if (_taskCompletionSource != null) - _taskCompletionSource.TrySetResult(null); - } + FinishOperation(); } } @@ -214,9 +211,12 @@ namespace YooAsset /// internal void AbortOperation() { - foreach (var child in Childs) + if (_childs != null) { - child.AbortOperation(); + foreach (var child in _childs) + { + child.AbortOperation(); + } } if (IsDone == false) @@ -241,8 +241,20 @@ namespace YooAsset // 结束记录 DebugEndRecording(); - if (_taskCompletionSource != null) - _taskCompletionSource.TrySetResult(null); + try + { + _callback?.Invoke(this); + } + catch (Exception ex) + { + YooLogger.Error($"Exception in completion callback: {ex}"); + } + finally + { + _callback = null; + if (_taskCompletionSource != null) + _taskCompletionSource.TrySetResult(null); + } } } @@ -268,14 +280,6 @@ namespace YooAsset return IsDone; } - /// - /// 清空完成回调 - /// - protected void ClearCompletedCallback() - { - _callback = null; - } - /// /// 等待异步执行完毕 /// @@ -295,6 +299,11 @@ namespace YooAsset { IsWaitForAsyncComplete = true; InternalWaitForAsyncComplete(); + +#if UNITY_EDITOR + if (IsDone == false) + throw new YooInternalException($"WaitForAsyncComplete() must complete operation: {this.GetType().Name}"); +#endif } } @@ -359,12 +368,21 @@ namespace YooAsset operationInfo.BeginTime = BeginTime; operationInfo.ProcessTime = ProcessTime; operationInfo.Status = Status.ToString(); - operationInfo.Childs = new List(Childs.Count); - foreach (var child in Childs) + + if (_childs == null) { - var childInfo = child.GetDebugOperationInfo(); - operationInfo.Childs.Add(childInfo); + operationInfo.Childs = new List(); } + else + { + operationInfo.Childs = new List(_childs.Count); + foreach (var child in _childs) + { + var childInfo = child.GetDebugOperationInfo(); + operationInfo.Childs.Add(childInfo); + } + } + return operationInfo; } #endregion diff --git a/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs b/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs index 8fe48872..806bbac9 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/GameAsyncOperation.cs @@ -11,14 +11,6 @@ namespace YooAsset { OnUpdate(); } - internal override void InternalAbort() - { - OnAbort(); - } - internal override void InternalWaitForAsyncComplete() - { - OnWaitForAsyncComplete(); - } /// /// 异步操作开始 @@ -29,31 +21,5 @@ namespace YooAsset /// 异步操作更新 /// protected abstract void OnUpdate(); - - /// - /// 异步操作终止 - /// - protected abstract void OnAbort(); - - /// - /// 异步等待完成 - /// - protected virtual void OnWaitForAsyncComplete() { } - - /// - /// 异步操作系统是否繁忙 - /// - protected bool IsBusy() - { - return OperationSystem.IsBusy; - } - - /// - /// 终止异步操作 - /// - protected void Abort() - { - AbortOperation(); - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs index ca4a13d9..9aa01a4a 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs @@ -40,11 +40,8 @@ namespace YooAsset _loopCounter--; LoopUnloadUnused(); - if (IsWaitForAsyncComplete == false) - { - if (OperationSystem.IsBusy) - break; - } + if (IsBusy) + break; } if (_loopCounter <= 0) diff --git a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs index c677f6db..d354e63c 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/Operation/Internal/DeserializeManifestOperation.cs @@ -159,11 +159,8 @@ namespace YooAsset _packageAssetCount--; Progress = 1f - _packageAssetCount / _progressTotalValue; - if (IsWaitForAsyncComplete == false) - { - if (OperationSystem.IsBusy) - break; - } + if (IsBusy) + break; } if (_packageAssetCount <= 0) @@ -196,11 +193,8 @@ namespace YooAsset _packageBundleCount--; Progress = 1f - _packageBundleCount / _progressTotalValue; - if (IsWaitForAsyncComplete == false) - { - if (OperationSystem.IsBusy) - break; - } + if (IsBusy) + break; } if (_packageBundleCount <= 0)