TaskManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /// Simple, really. There is no need to initialize or even refer to TaskManager.
  2. /// When the first Task is created in an application, a "TaskManager" GameObject
  3. /// will automatically be added to the scene root with the TaskManager component
  4. /// attached. This component will be responsible for dispatching all coroutines
  5. /// behind the scenes.
  6. ///
  7. /// Task also provides an event that is triggered when the coroutine exits.
  8. using UnityEngine;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System;
  12. namespace RayNeo.Native
  13. {
  14. /// A Task object represents a coroutine. Tasks can be started, paused, and stopped.
  15. /// It is an error to attempt to start a task that has been stopped or which has
  16. /// naturally terminated.
  17. public class Task
  18. {
  19. public static Task CreateTask(IEnumerator c, bool autoStart = true)
  20. {
  21. return new Task(c, autoStart);
  22. }
  23. /// Returns true if and only if the coroutine is running. Paused tasks
  24. /// are considered to be running.
  25. public bool Running
  26. {
  27. get
  28. {
  29. return m_Task.Running;
  30. }
  31. }
  32. /// Returns true if and only if the coroutine is currently paused.
  33. public bool Paused
  34. {
  35. get
  36. {
  37. return m_Task.Paused;
  38. }
  39. }
  40. /// Delegate for termination subscribers. manual is true if and only if
  41. /// the coroutine was stopped with an explicit call to Stop().
  42. public delegate void FinishedHandler(Task task, bool manual);
  43. /// Termination event. Triggered when the coroutine completes execution.
  44. public event FinishedHandler Finished;
  45. /// Creates a new Task object for the given coroutine.
  46. ///
  47. /// If autoStart is true (default) the task is automatically started
  48. /// upon construction.
  49. public Task(IEnumerator c, bool autoStart = true)
  50. {
  51. m_Task = TaskManager.CreateTask(c);
  52. m_Task.Finished += TaskFinished;
  53. if (autoStart)
  54. {
  55. Start();
  56. }
  57. }
  58. /// Begins execution of the coroutine
  59. public void Start()
  60. {
  61. m_Task.Start();
  62. }
  63. /// Discontinues execution of the coroutine at its next yield.
  64. public void Stop()
  65. {
  66. m_Task.Stop();
  67. }
  68. public void Pause()
  69. {
  70. m_Task.Pause();
  71. }
  72. public void Unpause()
  73. {
  74. m_Task.Unpause();
  75. }
  76. public Coroutine Coroutine
  77. {
  78. get
  79. {
  80. return m_Task.Coroutine;
  81. }
  82. }
  83. void TaskFinished(bool manual)
  84. {
  85. FinishedHandler handler = Finished;
  86. if (handler != null)
  87. {
  88. handler(this, manual);
  89. }
  90. }
  91. internal static void CreateTask(object loadScene)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. TaskManager.TaskState m_Task;
  96. }
  97. public class TaskManager : MonoBehaviour
  98. {
  99. public class TaskState
  100. {
  101. public bool Running
  102. {
  103. get
  104. {
  105. return m_Running;
  106. }
  107. }
  108. public bool Paused
  109. {
  110. get
  111. {
  112. return m_Paused;
  113. }
  114. }
  115. public Coroutine Coroutine
  116. {
  117. get
  118. {
  119. return m_Coroutine;
  120. }
  121. }
  122. public delegate void FinishedHandler(bool manual);
  123. public event FinishedHandler Finished;
  124. IEnumerator m_Enumerator;
  125. Coroutine m_Coroutine;
  126. bool m_Running;
  127. bool m_Paused;
  128. bool m_Stopped;
  129. public TaskState(IEnumerator c)
  130. {
  131. m_Enumerator = c;
  132. }
  133. public void Pause()
  134. {
  135. m_Paused = true;
  136. }
  137. public void Unpause()
  138. {
  139. m_Paused = false;
  140. }
  141. public void Start()
  142. {
  143. m_Running = true;
  144. m_Coroutine = m_Singleton.StartCoroutine(CallWrapper());
  145. }
  146. public void Stop()
  147. {
  148. m_Stopped = true;
  149. m_Running = false;
  150. }
  151. private IEnumerator CallWrapper()
  152. {
  153. //yield return null;
  154. IEnumerator e = m_Enumerator;
  155. while (m_Running)
  156. {
  157. if (m_Paused)
  158. {
  159. yield return null;
  160. }
  161. else
  162. {
  163. if (e != null && e.MoveNext())
  164. {
  165. yield return e.Current;
  166. }
  167. else
  168. {
  169. m_Running = false;
  170. }
  171. }
  172. }
  173. FinishedHandler handler = Finished;
  174. if (handler != null)
  175. {
  176. handler(m_Stopped);
  177. }
  178. }
  179. }
  180. private static TaskManager m_Singleton;
  181. public static TaskState CreateTask(IEnumerator coroutine)
  182. {
  183. if (m_Singleton == null)
  184. {
  185. GameObject go = new GameObject("TaskManager");
  186. m_Singleton = go.AddComponent<TaskManager>();
  187. DontDestroyOnLoad(go);
  188. }
  189. return new TaskState(coroutine);
  190. }
  191. }
  192. /// <summary>
  193. /// 任务队列
  194. /// 在某个时间点最多只有一个协程在执行,先加入队列中的先执行,后加入的后执行
  195. /// </summary>
  196. public class TaskQueue
  197. {
  198. static Dictionary<string, TaskQueue> m_TaskQueues = new Dictionary<string, TaskQueue>();
  199. public static TaskQueue GetTaskQueue(string id)
  200. {
  201. TaskQueue taskQueue;
  202. if (!m_TaskQueues.TryGetValue(id, out taskQueue))
  203. {
  204. taskQueue = new TaskQueue();
  205. m_TaskQueues[id] = taskQueue;
  206. }
  207. return taskQueue;
  208. }
  209. public static void RemoveTaskQueue(string id)
  210. {
  211. if (m_TaskQueues.ContainsKey(id))
  212. {
  213. m_TaskQueues.Remove(id);
  214. }
  215. }
  216. private List<TaskInfo> m_ListTask = new List<TaskInfo>();
  217. public delegate void DelegateVoid();
  218. public DelegateVoid OnTaskQueueFinished;
  219. public TaskInfo Add(IEnumerator c)
  220. {
  221. TaskInfo taskInfo = new TaskInfo();
  222. taskInfo.c = c;
  223. m_ListTask.Add(taskInfo);
  224. if (m_ListTask.Count == 1)
  225. {
  226. Run(m_ListTask[0]);
  227. }
  228. return taskInfo;
  229. }
  230. public void Remove(TaskInfo taskInfo)
  231. {
  232. if (m_ListTask.Contains(taskInfo))
  233. {
  234. bool removeTaskIsRuning = false;
  235. if (taskInfo.task != null)
  236. {
  237. taskInfo.task.Stop();
  238. removeTaskIsRuning = true;
  239. }
  240. m_ListTask.Remove(taskInfo);
  241. if (removeTaskIsRuning && m_ListTask.Count > 0)
  242. {
  243. Run(m_ListTask[0]);
  244. }
  245. }
  246. }
  247. public void Clear()
  248. {
  249. for (int i = 0; i < m_ListTask.Count; i++)
  250. {
  251. if (m_ListTask[i].task != null)
  252. {
  253. m_ListTask[i].task.Stop();
  254. }
  255. }
  256. m_ListTask.Clear();
  257. }
  258. void OnFinish(Task task, bool manual)
  259. {
  260. if (m_ListTask.Count > 0)
  261. {
  262. m_ListTask.RemoveAt(0);
  263. if (m_ListTask.Count > 0)
  264. {
  265. Run(m_ListTask[0]);
  266. }
  267. else
  268. {
  269. if (OnTaskQueueFinished != null)
  270. {
  271. OnTaskQueueFinished();
  272. }
  273. }
  274. }
  275. }
  276. void Run(TaskInfo taskInfo)
  277. {
  278. taskInfo.task = new Task(taskInfo.c);
  279. taskInfo.task.Finished += OnFinish;
  280. }
  281. public class TaskInfo
  282. {
  283. public IEnumerator c;
  284. public Task task;
  285. }
  286. }
  287. }