using System.Collections; using System.Collections.Generic; using System.Diagnostics; namespace YooAsset { internal class OperationSystem { private static readonly List _operations = new List(100); // 计时器相关 private static Stopwatch _watch; private static long _frameTime; /// /// 异步操作的最小时间片段 /// public static long MaxTimeSlice { set; get; } = long.MaxValue; /// /// 处理器是否繁忙 /// public static bool IsBusy { get { return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice; } } /// /// 初始化异步操作系统 /// public static void Initialize() { _watch = Stopwatch.StartNew(); } /// /// 更新异步操作系统 /// public static void Update() { _frameTime = _watch.ElapsedMilliseconds; for (int i = _operations.Count - 1; i >= 0; i--) { if (IsBusy) return; var operation = _operations[i]; operation.Update(); if (operation.IsDone) { _operations.RemoveAt(i); operation.Finish(); } } } /// /// 销毁异步操作系统 /// public static void DestroyAll() { _operations.Clear(); _watch = null; _frameTime = 0; MaxTimeSlice = long.MaxValue; } /// /// 开始处理异步操作类 /// public static void StartOperation(AsyncOperationBase operationBase) { _operations.Add(operationBase); operationBase.Start(); } } }