Timer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using System.Threading;
  3. using UnityEngine;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<AsyncUnit> Timer(TimeSpan dueTime, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false)
  9. {
  10. return new Timer(dueTime, null, updateTiming, ignoreTimeScale);
  11. }
  12. public static IUniTaskAsyncEnumerable<AsyncUnit> Timer(TimeSpan dueTime, TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false)
  13. {
  14. return new Timer(dueTime, period, updateTiming, ignoreTimeScale);
  15. }
  16. public static IUniTaskAsyncEnumerable<AsyncUnit> Interval(TimeSpan period, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update, bool ignoreTimeScale = false)
  17. {
  18. return new Timer(period, period, updateTiming, ignoreTimeScale);
  19. }
  20. public static IUniTaskAsyncEnumerable<AsyncUnit> TimerFrame(int dueTimeFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
  21. {
  22. if (dueTimeFrameCount < 0)
  23. {
  24. throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount);
  25. }
  26. return new TimerFrame(dueTimeFrameCount, null, updateTiming);
  27. }
  28. public static IUniTaskAsyncEnumerable<AsyncUnit> TimerFrame(int dueTimeFrameCount, int periodFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
  29. {
  30. if (dueTimeFrameCount < 0)
  31. {
  32. throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. dueTimeFrameCount:" + dueTimeFrameCount);
  33. }
  34. if (periodFrameCount < 0)
  35. {
  36. throw new ArgumentOutOfRangeException("Delay does not allow minus periodFrameCount. periodFrameCount:" + dueTimeFrameCount);
  37. }
  38. return new TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming);
  39. }
  40. public static IUniTaskAsyncEnumerable<AsyncUnit> IntervalFrame(int intervalFrameCount, PlayerLoopTiming updateTiming = PlayerLoopTiming.Update)
  41. {
  42. if (intervalFrameCount < 0)
  43. {
  44. throw new ArgumentOutOfRangeException("Delay does not allow minus intervalFrameCount. intervalFrameCount:" + intervalFrameCount);
  45. }
  46. return new TimerFrame(intervalFrameCount, intervalFrameCount, updateTiming);
  47. }
  48. }
  49. internal class Timer : IUniTaskAsyncEnumerable<AsyncUnit>
  50. {
  51. readonly PlayerLoopTiming updateTiming;
  52. readonly TimeSpan dueTime;
  53. readonly TimeSpan? period;
  54. readonly bool ignoreTimeScale;
  55. public Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale)
  56. {
  57. this.updateTiming = updateTiming;
  58. this.dueTime = dueTime;
  59. this.period = period;
  60. this.ignoreTimeScale = ignoreTimeScale;
  61. }
  62. public IUniTaskAsyncEnumerator<AsyncUnit> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  63. {
  64. return new _Timer(dueTime, period, updateTiming, ignoreTimeScale, cancellationToken);
  65. }
  66. class _Timer : MoveNextSource, IUniTaskAsyncEnumerator<AsyncUnit>, IPlayerLoopItem
  67. {
  68. readonly float dueTime;
  69. readonly float? period;
  70. readonly PlayerLoopTiming updateTiming;
  71. readonly bool ignoreTimeScale;
  72. CancellationToken cancellationToken;
  73. int initialFrame;
  74. float elapsed;
  75. bool dueTimePhase;
  76. bool completed;
  77. bool disposed;
  78. public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, CancellationToken cancellationToken)
  79. {
  80. this.dueTime = (float)dueTime.TotalSeconds;
  81. this.period = (period == null) ? null : (float?)period.Value.TotalSeconds;
  82. if (this.dueTime <= 0) this.dueTime = 0;
  83. if (this.period != null)
  84. {
  85. if (this.period <= 0) this.period = 1;
  86. }
  87. this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
  88. this.dueTimePhase = true;
  89. this.updateTiming = updateTiming;
  90. this.ignoreTimeScale = ignoreTimeScale;
  91. this.cancellationToken = cancellationToken;
  92. TaskTracker.TrackActiveTask(this, 2);
  93. PlayerLoopHelper.AddAction(updateTiming, this);
  94. }
  95. public AsyncUnit Current => default;
  96. public UniTask<bool> MoveNextAsync()
  97. {
  98. // return false instead of throw
  99. if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False;
  100. // reset value here.
  101. this.elapsed = 0;
  102. completionSource.Reset();
  103. return new UniTask<bool>(this, completionSource.Version);
  104. }
  105. public UniTask DisposeAsync()
  106. {
  107. if (!disposed)
  108. {
  109. disposed = true;
  110. TaskTracker.RemoveTracking(this);
  111. }
  112. return default;
  113. }
  114. public bool MoveNext()
  115. {
  116. if (disposed || cancellationToken.IsCancellationRequested)
  117. {
  118. completionSource.TrySetResult(false);
  119. return false;
  120. }
  121. if (dueTimePhase)
  122. {
  123. if (elapsed == 0)
  124. {
  125. // skip in initial frame.
  126. if (initialFrame == Time.frameCount)
  127. {
  128. return true;
  129. }
  130. }
  131. elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime;
  132. if (elapsed >= dueTime)
  133. {
  134. dueTimePhase = false;
  135. completionSource.TrySetResult(true);
  136. }
  137. }
  138. else
  139. {
  140. if (period == null)
  141. {
  142. completed = true;
  143. completionSource.TrySetResult(false);
  144. return false;
  145. }
  146. elapsed += (ignoreTimeScale) ? UnityEngine.Time.unscaledDeltaTime : UnityEngine.Time.deltaTime;
  147. if (elapsed >= period)
  148. {
  149. completionSource.TrySetResult(true);
  150. }
  151. }
  152. return true;
  153. }
  154. }
  155. }
  156. internal class TimerFrame : IUniTaskAsyncEnumerable<AsyncUnit>
  157. {
  158. readonly PlayerLoopTiming updateTiming;
  159. readonly int dueTimeFrameCount;
  160. readonly int? periodFrameCount;
  161. public TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming)
  162. {
  163. this.updateTiming = updateTiming;
  164. this.dueTimeFrameCount = dueTimeFrameCount;
  165. this.periodFrameCount = periodFrameCount;
  166. }
  167. public IUniTaskAsyncEnumerator<AsyncUnit> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  168. {
  169. return new _TimerFrame(dueTimeFrameCount, periodFrameCount, updateTiming, cancellationToken);
  170. }
  171. class _TimerFrame : MoveNextSource, IUniTaskAsyncEnumerator<AsyncUnit>, IPlayerLoopItem
  172. {
  173. readonly int dueTimeFrameCount;
  174. readonly int? periodFrameCount;
  175. CancellationToken cancellationToken;
  176. int initialFrame;
  177. int currentFrame;
  178. bool dueTimePhase;
  179. bool completed;
  180. bool disposed;
  181. public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, CancellationToken cancellationToken)
  182. {
  183. if (dueTimeFrameCount <= 0) dueTimeFrameCount = 0;
  184. if (periodFrameCount != null)
  185. {
  186. if (periodFrameCount <= 0) periodFrameCount = 1;
  187. }
  188. this.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
  189. this.dueTimePhase = true;
  190. this.dueTimeFrameCount = dueTimeFrameCount;
  191. this.periodFrameCount = periodFrameCount;
  192. this.cancellationToken = cancellationToken;
  193. TaskTracker.TrackActiveTask(this, 2);
  194. PlayerLoopHelper.AddAction(updateTiming, this);
  195. }
  196. public AsyncUnit Current => default;
  197. public UniTask<bool> MoveNextAsync()
  198. {
  199. // return false instead of throw
  200. if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False;
  201. // reset value here.
  202. this.currentFrame = 0;
  203. completionSource.Reset();
  204. return new UniTask<bool>(this, completionSource.Version);
  205. }
  206. public UniTask DisposeAsync()
  207. {
  208. if (!disposed)
  209. {
  210. disposed = true;
  211. TaskTracker.RemoveTracking(this);
  212. }
  213. return default;
  214. }
  215. public bool MoveNext()
  216. {
  217. if (disposed || cancellationToken.IsCancellationRequested)
  218. {
  219. completionSource.TrySetResult(false);
  220. return false;
  221. }
  222. if (dueTimePhase)
  223. {
  224. if (currentFrame == 0)
  225. {
  226. if (dueTimeFrameCount == 0)
  227. {
  228. dueTimePhase = false;
  229. completionSource.TrySetResult(true);
  230. return true;
  231. }
  232. // skip in initial frame.
  233. if (initialFrame == Time.frameCount)
  234. {
  235. return true;
  236. }
  237. }
  238. if (++currentFrame >= dueTimeFrameCount)
  239. {
  240. dueTimePhase = false;
  241. completionSource.TrySetResult(true);
  242. }
  243. else
  244. {
  245. }
  246. }
  247. else
  248. {
  249. if (periodFrameCount == null)
  250. {
  251. completed = true;
  252. completionSource.TrySetResult(false);
  253. return false;
  254. }
  255. if (++currentFrame >= periodFrameCount)
  256. {
  257. completionSource.TrySetResult(true);
  258. }
  259. }
  260. return true;
  261. }
  262. }
  263. }
  264. }