OperationSystem.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace YooAsset
  5. {
  6. internal class OperationSystem
  7. {
  8. #if UNITY_EDITOR
  9. [UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.SubsystemRegistration)]
  10. private static void OnRuntimeInitialize()
  11. {
  12. DestroyAll();
  13. }
  14. #endif
  15. private static readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(1000);
  16. private static readonly List<AsyncOperationBase> _newList = new List<AsyncOperationBase>(1000);
  17. // 计时器相关
  18. private static Stopwatch _watch;
  19. private static long _frameTime;
  20. /// <summary>
  21. /// 异步操作的最小时间片段
  22. /// </summary>
  23. public static long MaxTimeSlice { set; get; } = long.MaxValue;
  24. /// <summary>
  25. /// 处理器是否繁忙
  26. /// </summary>
  27. public static bool IsBusy
  28. {
  29. get
  30. {
  31. return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
  32. }
  33. }
  34. /// <summary>
  35. /// 初始化异步操作系统
  36. /// </summary>
  37. public static void Initialize()
  38. {
  39. _watch = Stopwatch.StartNew();
  40. }
  41. /// <summary>
  42. /// 更新异步操作系统
  43. /// </summary>
  44. public static void Update()
  45. {
  46. // 移除已经完成的异步操作
  47. // 注意:移除上一帧完成的异步操作,方便调试器接收到完整的信息!
  48. for (int i = _operations.Count - 1; i >= 0; i--)
  49. {
  50. var operation = _operations[i];
  51. if (operation.IsFinish)
  52. _operations.RemoveAt(i);
  53. }
  54. // 添加新增的异步操作
  55. if (_newList.Count > 0)
  56. {
  57. bool sorting = false;
  58. foreach (var operation in _newList)
  59. {
  60. if (operation.Priority > 0)
  61. {
  62. sorting = true;
  63. break;
  64. }
  65. }
  66. _operations.AddRange(_newList);
  67. _newList.Clear();
  68. // 重新排序优先级
  69. if (sorting)
  70. _operations.Sort();
  71. }
  72. // 更新进行中的异步操作
  73. _frameTime = _watch.ElapsedMilliseconds;
  74. for (int i = 0; i < _operations.Count; i++)
  75. {
  76. if (IsBusy)
  77. break;
  78. var operation = _operations[i];
  79. if (operation.IsFinish)
  80. continue;
  81. operation.UpdateOperation();
  82. }
  83. }
  84. /// <summary>
  85. /// 销毁异步操作系统
  86. /// </summary>
  87. public static void DestroyAll()
  88. {
  89. _operations.Clear();
  90. _newList.Clear();
  91. _watch = null;
  92. _frameTime = 0;
  93. MaxTimeSlice = long.MaxValue;
  94. }
  95. /// <summary>
  96. /// 销毁包裹的所有任务
  97. /// </summary>
  98. public static void ClearPackageOperation(string packageName)
  99. {
  100. // 终止临时队列里的任务
  101. foreach (var operation in _newList)
  102. {
  103. if (operation.PackageName == packageName)
  104. {
  105. operation.AbortOperation();
  106. }
  107. }
  108. // 终止正在进行的任务
  109. foreach (var operation in _operations)
  110. {
  111. if (operation.PackageName == packageName)
  112. {
  113. operation.AbortOperation();
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// 开始处理异步操作类
  119. /// </summary>
  120. public static void StartOperation(string packageName, AsyncOperationBase operation)
  121. {
  122. _newList.Add(operation);
  123. operation.SetPackageName(packageName);
  124. operation.StartOperation();
  125. }
  126. #region 调试信息
  127. internal static List<DebugOperationInfo> GetDebugOperationInfos(string packageName)
  128. {
  129. List<DebugOperationInfo> result = new List<DebugOperationInfo>(_operations.Count);
  130. foreach (var operation in _operations)
  131. {
  132. if (operation.PackageName == packageName)
  133. {
  134. var operationInfo = GetDebugOperationInfo(operation);
  135. result.Add(operationInfo);
  136. }
  137. }
  138. return result;
  139. }
  140. internal static DebugOperationInfo GetDebugOperationInfo(AsyncOperationBase operation)
  141. {
  142. var operationInfo = new DebugOperationInfo();
  143. operationInfo.OperationName = operation.GetType().Name;
  144. operationInfo.OperationDesc = operation.GetOperationDesc();
  145. operationInfo.Priority = operation.Priority;
  146. operationInfo.Progress = operation.Progress;
  147. operationInfo.BeginTime = operation.BeginTime;
  148. operationInfo.ProcessTime = operation.ProcessTime;
  149. operationInfo.Status = operation.Status.ToString();
  150. operationInfo.Childs = new List<DebugOperationInfo>(operation.Childs.Count);
  151. foreach (var child in operation.Childs)
  152. {
  153. var childInfo = GetDebugOperationInfo(child);
  154. operationInfo.Childs.Add(childInfo);
  155. }
  156. return operationInfo;
  157. }
  158. #endregion
  159. }
  160. }