OperationSystem.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace YooAsset
  5. {
  6. internal class OperationSystem
  7. {
  8. private static readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(100);
  9. // 计时器相关
  10. private static Stopwatch _watch;
  11. private static long _maxTimeSlice;
  12. private static long _frameTime;
  13. /// <summary>
  14. /// 处理器是否繁忙
  15. /// </summary>
  16. public static bool IsBusy
  17. {
  18. get
  19. {
  20. return _watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice;
  21. }
  22. }
  23. /// <summary>
  24. /// 初始化异步操作系统
  25. /// </summary>
  26. public static void Initialize(long maxTimeSlice)
  27. {
  28. _maxTimeSlice = maxTimeSlice;
  29. _watch = Stopwatch.StartNew();
  30. }
  31. /// <summary>
  32. /// 更新异步操作系统
  33. /// </summary>
  34. public static void Update()
  35. {
  36. _frameTime = _watch.ElapsedMilliseconds;
  37. for (int i = _operations.Count - 1; i >= 0; i--)
  38. {
  39. if (IsBusy)
  40. return;
  41. var operation = _operations[i];
  42. operation.Update();
  43. if (operation.IsDone)
  44. {
  45. _operations.RemoveAt(i);
  46. operation.Finish();
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 销毁异步操作系统
  52. /// </summary>
  53. public static void DestroyAll()
  54. {
  55. _operations.Clear();
  56. _watch = null;
  57. _maxTimeSlice = 0;
  58. _frameTime = 0;
  59. }
  60. /// <summary>
  61. /// 开始处理异步操作类
  62. /// </summary>
  63. public static void StartOperation(AsyncOperationBase operationBase)
  64. {
  65. _operations.Add(operationBase);
  66. operationBase.Start();
  67. }
  68. }
  69. }