AwaitableMotionConfiguredSource.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #if UNITY_2023_1_OR_NEWER
  2. using System.Threading;
  3. using UnityEngine;
  4. namespace LitMotion
  5. {
  6. // TODO: use object pool
  7. internal sealed class AwaitableMotionConfiguredSource : MotionConfiguredSourceBase
  8. {
  9. public static AwaitableMotionConfiguredSource CompletedSource
  10. {
  11. get
  12. {
  13. if (completedSource == null)
  14. {
  15. completedSource = new();
  16. }
  17. completedSource.core.Reset();
  18. completedSource.core.SetResult();
  19. return completedSource;
  20. }
  21. }
  22. static AwaitableMotionConfiguredSource completedSource;
  23. public static AwaitableMotionConfiguredSource CanceledSource
  24. {
  25. get
  26. {
  27. if (canceledSource == null)
  28. {
  29. canceledSource = new();
  30. }
  31. canceledSource.core.Reset();
  32. canceledSource.core.SetCanceled();
  33. return canceledSource;
  34. }
  35. }
  36. static AwaitableMotionConfiguredSource canceledSource;
  37. readonly AwaitableCompletionSource core = new();
  38. public Awaitable Awaitable => core.Awaitable;
  39. AwaitableMotionConfiguredSource() : base() { }
  40. public static AwaitableMotionConfiguredSource Create(MotionHandle motionHandle, CancelBehaviour cancelBehaviour, CancellationToken cancellationToken)
  41. {
  42. if (cancellationToken.IsCancellationRequested)
  43. {
  44. OnCanceledTokenReceived(motionHandle, cancelBehaviour);
  45. return CanceledSource;
  46. }
  47. var result = new AwaitableMotionConfiguredSource();
  48. result.Initialize(motionHandle, cancelBehaviour, cancellationToken);
  49. return result;
  50. }
  51. protected override void SetTaskCanceled(CancellationToken cancellationToken)
  52. {
  53. core.SetCanceled();
  54. }
  55. protected override void SetTaskCompleted()
  56. {
  57. core.SetResult();
  58. }
  59. }
  60. }
  61. #endif