Unity 框架

DOTweenAsyncExtensions.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // asmdef Version Defines, enabled when com.demigiant.dotween is imported.
  2. #if UNITASK_DOTWEEN_SUPPORT
  3. using Cysharp.Threading.Tasks.Internal;
  4. using DG.Tweening;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Runtime.CompilerServices;
  8. using System.Threading;
  9. namespace Cysharp.Threading.Tasks
  10. {
  11. public enum TweenCancelBehaviour
  12. {
  13. Kill,
  14. KillWithCompleteCallback,
  15. Complete,
  16. CompleteWithSequenceCallback,
  17. CancelAwait,
  18. // AndCancelAwait
  19. KillAndCancelAwait,
  20. KillWithCompleteCallbackAndCancelAwait,
  21. CompleteAndCancelAwait,
  22. CompleteWithSequenceCallbackAndCancelAwait
  23. }
  24. public static class DOTweenAsyncExtensions
  25. {
  26. enum CallbackType
  27. {
  28. Kill,
  29. Complete,
  30. Pause,
  31. Play,
  32. Rewind,
  33. StepComplete
  34. }
  35. public static TweenAwaiter GetAwaiter(this Tween tween)
  36. {
  37. return new TweenAwaiter(tween);
  38. }
  39. public static UniTask WithCancellation(this Tween tween, CancellationToken cancellationToken)
  40. {
  41. Error.ThrowArgumentNullException(tween, nameof(tween));
  42. if (!tween.IsActive()) return UniTask.CompletedTask;
  43. return new UniTask(TweenConfiguredSource.Create(tween, TweenCancelBehaviour.Kill, cancellationToken, CallbackType.Kill, out var token), token);
  44. }
  45. public static UniTask ToUniTask(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
  46. {
  47. Error.ThrowArgumentNullException(tween, nameof(tween));
  48. if (!tween.IsActive()) return UniTask.CompletedTask;
  49. return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Kill, out var token), token);
  50. }
  51. public static UniTask AwaitForComplete(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
  52. {
  53. Error.ThrowArgumentNullException(tween, nameof(tween));
  54. if (!tween.IsActive()) return UniTask.CompletedTask;
  55. return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Complete, out var token), token);
  56. }
  57. public static UniTask AwaitForPause(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
  58. {
  59. Error.ThrowArgumentNullException(tween, nameof(tween));
  60. if (!tween.IsActive()) return UniTask.CompletedTask;
  61. return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Pause, out var token), token);
  62. }
  63. public static UniTask AwaitForPlay(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
  64. {
  65. Error.ThrowArgumentNullException(tween, nameof(tween));
  66. if (!tween.IsActive()) return UniTask.CompletedTask;
  67. return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Play, out var token), token);
  68. }
  69. public static UniTask AwaitForRewind(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
  70. {
  71. Error.ThrowArgumentNullException(tween, nameof(tween));
  72. if (!tween.IsActive()) return UniTask.CompletedTask;
  73. return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.Rewind, out var token), token);
  74. }
  75. public static UniTask AwaitForStepComplete(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
  76. {
  77. Error.ThrowArgumentNullException(tween, nameof(tween));
  78. if (!tween.IsActive()) return UniTask.CompletedTask;
  79. return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, CallbackType.StepComplete, out var token), token);
  80. }
  81. public struct TweenAwaiter : ICriticalNotifyCompletion
  82. {
  83. readonly Tween tween;
  84. // killed(non active) as completed.
  85. public bool IsCompleted => !tween.IsActive();
  86. public TweenAwaiter(Tween tween)
  87. {
  88. this.tween = tween;
  89. }
  90. public TweenAwaiter GetAwaiter()
  91. {
  92. return this;
  93. }
  94. public void GetResult()
  95. {
  96. }
  97. public void OnCompleted(System.Action continuation)
  98. {
  99. UnsafeOnCompleted(continuation);
  100. }
  101. public void UnsafeOnCompleted(System.Action continuation)
  102. {
  103. // onKill is called after OnCompleted, both Complete(false/true) and Kill(false/true).
  104. tween.onKill = PooledTweenCallback.Create(continuation);
  105. }
  106. }
  107. sealed class TweenConfiguredSource : IUniTaskSource, ITaskPoolNode<TweenConfiguredSource>
  108. {
  109. static TaskPool<TweenConfiguredSource> pool;
  110. TweenConfiguredSource nextNode;
  111. public ref TweenConfiguredSource NextNode => ref nextNode;
  112. static TweenConfiguredSource()
  113. {
  114. TaskPool.RegisterSizeGetter(typeof(TweenConfiguredSource), () => pool.Size);
  115. }
  116. readonly TweenCallback onCompleteCallbackDelegate;
  117. Tween tween;
  118. TweenCancelBehaviour cancelBehaviour;
  119. CancellationToken cancellationToken;
  120. CancellationTokenRegistration cancellationRegistration;
  121. CallbackType callbackType;
  122. bool canceled;
  123. TweenCallback originalCompleteAction;
  124. UniTaskCompletionSourceCore<AsyncUnit> core;
  125. TweenConfiguredSource()
  126. {
  127. onCompleteCallbackDelegate = OnCompleteCallbackDelegate;
  128. }
  129. public static IUniTaskSource Create(Tween tween, TweenCancelBehaviour cancelBehaviour, CancellationToken cancellationToken, CallbackType callbackType, out short token)
  130. {
  131. if (cancellationToken.IsCancellationRequested)
  132. {
  133. DoCancelBeforeCreate(tween, cancelBehaviour);
  134. return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
  135. }
  136. if (!pool.TryPop(out var result))
  137. {
  138. result = new TweenConfiguredSource();
  139. }
  140. result.tween = tween;
  141. result.cancelBehaviour = cancelBehaviour;
  142. result.cancellationToken = cancellationToken;
  143. result.callbackType = callbackType;
  144. result.canceled = false;
  145. switch (callbackType)
  146. {
  147. case CallbackType.Kill:
  148. result.originalCompleteAction = tween.onKill;
  149. tween.onKill = result.onCompleteCallbackDelegate;
  150. break;
  151. case CallbackType.Complete:
  152. result.originalCompleteAction = tween.onComplete;
  153. tween.onComplete = result.onCompleteCallbackDelegate;
  154. break;
  155. case CallbackType.Pause:
  156. result.originalCompleteAction = tween.onPause;
  157. tween.onPause = result.onCompleteCallbackDelegate;
  158. break;
  159. case CallbackType.Play:
  160. result.originalCompleteAction = tween.onPlay;
  161. tween.onPlay = result.onCompleteCallbackDelegate;
  162. break;
  163. case CallbackType.Rewind:
  164. result.originalCompleteAction = tween.onRewind;
  165. tween.onRewind = result.onCompleteCallbackDelegate;
  166. break;
  167. case CallbackType.StepComplete:
  168. result.originalCompleteAction = tween.onStepComplete;
  169. tween.onStepComplete = result.onCompleteCallbackDelegate;
  170. break;
  171. default:
  172. break;
  173. }
  174. if (result.originalCompleteAction == result.onCompleteCallbackDelegate)
  175. {
  176. result.originalCompleteAction = null;
  177. }
  178. if (cancellationToken.CanBeCanceled)
  179. {
  180. result.cancellationRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(x =>
  181. {
  182. var source = (TweenConfiguredSource)x;
  183. switch (source.cancelBehaviour)
  184. {
  185. case TweenCancelBehaviour.Kill:
  186. default:
  187. source.tween.Kill(false);
  188. break;
  189. case TweenCancelBehaviour.KillAndCancelAwait:
  190. source.canceled = true;
  191. source.tween.Kill(false);
  192. break;
  193. case TweenCancelBehaviour.KillWithCompleteCallback:
  194. source.tween.Kill(true);
  195. break;
  196. case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
  197. source.canceled = true;
  198. source.tween.Kill(true);
  199. break;
  200. case TweenCancelBehaviour.Complete:
  201. source.tween.Complete(false);
  202. break;
  203. case TweenCancelBehaviour.CompleteAndCancelAwait:
  204. source.canceled = true;
  205. source.tween.Complete(false);
  206. break;
  207. case TweenCancelBehaviour.CompleteWithSequenceCallback:
  208. source.tween.Complete(true);
  209. break;
  210. case TweenCancelBehaviour.CompleteWithSequenceCallbackAndCancelAwait:
  211. source.canceled = true;
  212. source.tween.Complete(true);
  213. break;
  214. case TweenCancelBehaviour.CancelAwait:
  215. source.RestoreOriginalCallback();
  216. source.core.TrySetCanceled(source.cancellationToken);
  217. break;
  218. }
  219. }, result);
  220. }
  221. TaskTracker.TrackActiveTask(result, 3);
  222. token = result.core.Version;
  223. return result;
  224. }
  225. void OnCompleteCallbackDelegate()
  226. {
  227. if (cancellationToken.IsCancellationRequested)
  228. {
  229. if (this.cancelBehaviour == TweenCancelBehaviour.KillAndCancelAwait
  230. || this.cancelBehaviour == TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait
  231. || this.cancelBehaviour == TweenCancelBehaviour.CompleteAndCancelAwait
  232. || this.cancelBehaviour == TweenCancelBehaviour.CompleteWithSequenceCallbackAndCancelAwait
  233. || this.cancelBehaviour == TweenCancelBehaviour.CancelAwait)
  234. {
  235. canceled = true;
  236. }
  237. }
  238. if (canceled)
  239. {
  240. core.TrySetCanceled(cancellationToken);
  241. }
  242. else
  243. {
  244. originalCompleteAction?.Invoke();
  245. core.TrySetResult(AsyncUnit.Default);
  246. }
  247. }
  248. static void DoCancelBeforeCreate(Tween tween, TweenCancelBehaviour tweenCancelBehaviour)
  249. {
  250. switch (tweenCancelBehaviour)
  251. {
  252. case TweenCancelBehaviour.Kill:
  253. default:
  254. tween.Kill(false);
  255. break;
  256. case TweenCancelBehaviour.KillAndCancelAwait:
  257. tween.Kill(false);
  258. break;
  259. case TweenCancelBehaviour.KillWithCompleteCallback:
  260. tween.Kill(true);
  261. break;
  262. case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
  263. tween.Kill(true);
  264. break;
  265. case TweenCancelBehaviour.Complete:
  266. tween.Complete(false);
  267. break;
  268. case TweenCancelBehaviour.CompleteAndCancelAwait:
  269. tween.Complete(false);
  270. break;
  271. case TweenCancelBehaviour.CompleteWithSequenceCallback:
  272. tween.Complete(true);
  273. break;
  274. case TweenCancelBehaviour.CompleteWithSequenceCallbackAndCancelAwait:
  275. tween.Complete(true);
  276. break;
  277. case TweenCancelBehaviour.CancelAwait:
  278. break;
  279. }
  280. }
  281. public void GetResult(short token)
  282. {
  283. try
  284. {
  285. core.GetResult(token);
  286. }
  287. finally
  288. {
  289. TryReturn();
  290. }
  291. }
  292. public UniTaskStatus GetStatus(short token)
  293. {
  294. return core.GetStatus(token);
  295. }
  296. public UniTaskStatus UnsafeGetStatus()
  297. {
  298. return core.UnsafeGetStatus();
  299. }
  300. public void OnCompleted(Action<object> continuation, object state, short token)
  301. {
  302. core.OnCompleted(continuation, state, token);
  303. }
  304. bool TryReturn()
  305. {
  306. TaskTracker.RemoveTracking(this);
  307. core.Reset();
  308. cancellationRegistration.Dispose();
  309. RestoreOriginalCallback();
  310. tween = default;
  311. cancellationToken = default;
  312. originalCompleteAction = default;
  313. return pool.TryPush(this);
  314. }
  315. void RestoreOriginalCallback()
  316. {
  317. switch (callbackType)
  318. {
  319. case CallbackType.Kill:
  320. tween.onKill = originalCompleteAction;
  321. break;
  322. case CallbackType.Complete:
  323. tween.onComplete = originalCompleteAction;
  324. break;
  325. case CallbackType.Pause:
  326. tween.onPause = originalCompleteAction;
  327. break;
  328. case CallbackType.Play:
  329. tween.onPlay = originalCompleteAction;
  330. break;
  331. case CallbackType.Rewind:
  332. tween.onRewind = originalCompleteAction;
  333. break;
  334. case CallbackType.StepComplete:
  335. tween.onStepComplete = originalCompleteAction;
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. sealed class PooledTweenCallback
  344. {
  345. static readonly ConcurrentQueue<PooledTweenCallback> pool = new ConcurrentQueue<PooledTweenCallback>();
  346. readonly TweenCallback runDelegate;
  347. Action continuation;
  348. PooledTweenCallback()
  349. {
  350. runDelegate = Run;
  351. }
  352. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  353. public static TweenCallback Create(Action continuation)
  354. {
  355. if (!pool.TryDequeue(out var item))
  356. {
  357. item = new PooledTweenCallback();
  358. }
  359. item.continuation = continuation;
  360. return item.runDelegate;
  361. }
  362. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  363. void Run()
  364. {
  365. var call = continuation;
  366. continuation = null;
  367. if (call != null)
  368. {
  369. pool.Enqueue(this);
  370. call.Invoke();
  371. }
  372. }
  373. }
  374. }
  375. #endif