TimerComponent.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. namespace WS
  7. {
  8. [ObjectSystem]
  9. public class TimerComponentAwakeSystem : AwakeSystem<TimerComponent>
  10. {
  11. public override void Awake(TimerComponent self)
  12. {
  13. self.Awake();
  14. }
  15. }
  16. [ObjectSystem]
  17. public class TimerComponentUpdateSystem : UpdateSystem<TimerComponent>
  18. {
  19. public override void Update(TimerComponent self)
  20. {
  21. self.Update();
  22. }
  23. }
  24. ///<summary>时间管理器</summary>
  25. public class TimerComponent : SingleComponent<TimerComponent>
  26. {
  27. ///<summary>所有时间实体</summary>
  28. private List<TimerEntity> ListTimes = new List<TimerEntity>();
  29. ///<summary>协程载体</summary>
  30. private Init Mono;
  31. ///<summary>是否暂停</summary>
  32. public bool IsPause;
  33. public void Awake()
  34. {
  35. Mono = GameObject.FindObjectOfType<Init>();
  36. }
  37. public void Update()
  38. {
  39. if (!IsPause)
  40. {
  41. for (int i = ListTimes.Count - 1; i >= 0; i--)
  42. {
  43. if (!ListTimes[i].IsPause)
  44. {
  45. ListTimes[i].UseTime += Time.deltaTime;
  46. ListTimes[i].OnUpdate?.Invoke(ListTimes[i].UseTime);
  47. if (ListTimes[i].UseTime >= ListTimes[i].WaitTime && !ListTimes[i].IsPause)
  48. {
  49. if (ListTimes[i].Tcs == null)
  50. {
  51. ListTimes[i].OnCallback?.Invoke();
  52. if (ListTimes[i].IsLoop)
  53. {
  54. ListTimes[i].UseTime = 0;
  55. }
  56. else
  57. {
  58. ListTimes[i].Dispose();
  59. ListTimes.Remove(ListTimes[i]);
  60. }
  61. }
  62. else
  63. {
  64. ListTimes[i].Tcs.SetResult(true);
  65. ListTimes[i].Dispose();
  66. ListTimes.Remove(ListTimes[i]);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. ///<summary>异步时间等待器</summary>
  74. public Task WaitAsync(float time)
  75. {
  76. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  77. TimerEntity entity = ComponentFactory.CreateWithParent<TimerEntity>(this);
  78. entity.WaitTime = time;
  79. entity.Tcs = tcs;
  80. ListTimes.Add(entity);
  81. return tcs.Task;
  82. }
  83. /// <summary>创建时间等待器(是暂停状态需要手动开始)</summary>
  84. /// <param name="time">等待时间</param>
  85. /// <param name="oncallback">时间到后的回调</param>
  86. /// <param name="onupdate">帧逻辑</param>
  87. /// <param name="loop">循环</param>
  88. public TimerEntity Create(float time, Action oncallback = null, bool loop = false, Action<float> onupdate = null)
  89. {
  90. TimerEntity entity = ComponentFactory.CreateWithParent<TimerEntity>(this);
  91. entity.WaitTime = time;
  92. entity.OnCallback = oncallback;
  93. entity.OnUpdate = onupdate;
  94. entity.IsLoop = loop;
  95. entity.IsPause = true;
  96. ListTimes.Add(entity);
  97. return entity;
  98. }
  99. /// <summary>创建时间等待器(创建并执行)</summary>
  100. /// <param name="time">等待时间</param>
  101. /// <param name="oncallback">时间到后的回调</param>
  102. public TimerEntity Wait(float time, Action oncallback)
  103. {
  104. return Wait(time, oncallback, false, null);
  105. }
  106. /// <summary>创建时间等待器(创建并执行)</summary>
  107. /// <param name="time">等待时间</param>
  108. /// <param name="oncallback">时间到后的回调</param>
  109. /// <param name="loop">循环</param>
  110. public TimerEntity Wait(float time, Action oncallback, bool loop)
  111. {
  112. return Wait(time, oncallback, loop, null);
  113. }
  114. /// <summary>创建时间等待器(创建并执行)</summary>
  115. /// <param name="time">等待时间</param>
  116. /// <param name="oncallback">时间到后的回调</param>
  117. /// <param name="onupdate">帧逻辑</param>
  118. /// <param name="loop">循环</param>
  119. public TimerEntity Wait(float time, Action oncallback = null, bool loop = false, Action<float> onupdate = null)
  120. {
  121. TimerEntity entity = ComponentFactory.CreateWithParent<TimerEntity>(this);
  122. entity.WaitTime = time;
  123. entity.OnCallback = oncallback;
  124. entity.OnUpdate = onupdate;
  125. entity.IsLoop = loop;
  126. ListTimes.Add(entity);
  127. return entity;
  128. }
  129. ///<summary>停止并销毁</summary>
  130. public void Stop(TimerEntity entity)
  131. {
  132. if (ListTimes.Exists(t => t == entity))
  133. {
  134. entity.IsPause = true;
  135. entity.Dispose();
  136. ListTimes.Remove(entity);
  137. }
  138. }
  139. ///<summary>开启Mono协程(不受IsPause控制)</summary>
  140. public Coroutine StartCoroutine(IEnumerator routine)
  141. {
  142. return Mono.StartCoroutine(routine);
  143. }
  144. ///<summary>停止Mono协程</summary>
  145. public void StopCoroutine(IEnumerator routine)
  146. {
  147. Mono.StopCoroutine(routine);
  148. }
  149. public override void Dispose()
  150. {
  151. base.Dispose();
  152. IsPause = false;
  153. for (int i = 0; i < ListTimes.Count; i++)
  154. {
  155. ListTimes[i].Dispose();
  156. }
  157. ListTimes.Clear();
  158. Mono.StopAllCoroutines();
  159. Mono = null;
  160. }
  161. }
  162. }