From a98efd83b62e02a041af58d536d854de6bc4c36f Mon Sep 17 00:00:00 2001 From: hevinci Date: Tue, 31 Jan 2023 16:26:28 +0800 Subject: [PATCH] update operation system --- .../OperationSystem/OperationSystem.cs | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs index 85ce6fe..a4d5be1 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs @@ -7,6 +7,8 @@ namespace YooAsset internal class OperationSystem { private static readonly List _operations = new List(100); + private static readonly List _addList = new List(100); + private static readonly List _removeList = new List(100); // 计时器相关 private static Stopwatch _watch; @@ -44,19 +46,40 @@ namespace YooAsset { _frameTime = _watch.ElapsedMilliseconds; - for (int i = _operations.Count - 1; i >= 0; i--) + // 添加新的异步操作 + if (_addList.Count > 0) + { + for (int i = 0; i < _addList.Count; i++) + { + var operation = _addList[i]; + _operations.Add(operation); + } + _addList.Clear(); + } + + // 更新所有的异步操作 + foreach (var operation in _operations) { if (IsBusy) return; - var operation = _operations[i]; operation.Update(); if (operation.IsDone) { - _operations.RemoveAt(i); + _removeList.Add(operation); operation.Finish(); } } + + // 移除已经完成的异步操作 + if (_removeList.Count > 0) + { + foreach (var operation in _removeList) + { + _operations.Remove(operation); + } + _removeList.Clear(); + } } /// @@ -65,6 +88,8 @@ namespace YooAsset public static void DestroyAll() { _operations.Clear(); + _addList.Clear(); + _removeList.Clear(); _watch = null; _frameTime = 0; MaxTimeSlice = long.MaxValue; @@ -73,10 +98,10 @@ namespace YooAsset /// /// 开始处理异步操作类 /// - public static void StartOperation(AsyncOperationBase operationBase) + public static void StartOperation(AsyncOperationBase operation) { - _operations.Add(operationBase); - operationBase.Start(); + _addList.Add(operation); + operation.Start(); } } } \ No newline at end of file