LitMotionUniTaskExtensions.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #if LITMOTION_SUPPORT_UNITASK
  2. using System.Threading;
  3. using Cysharp.Threading.Tasks;
  4. namespace LitMotion
  5. {
  6. /// <summary>
  7. /// Provides extension methods for UniTask integration.
  8. /// </summary>
  9. public static class LitMotionUniTaskExtensions
  10. {
  11. /// <summary>
  12. /// Convert motion handle to UniTask.
  13. /// </summary>
  14. /// <param name="handle">This motion handle</param>
  15. /// <param name="cancellationToken">CancellationToken</param>
  16. /// <returns></returns>
  17. public static UniTask ToUniTask(this MotionHandle handle, CancellationToken cancellationToken = default)
  18. {
  19. if (!handle.IsActive()) return UniTask.CompletedTask;
  20. return new UniTask(UniTaskMotionConfiguredSource.Create(handle, CancelBehaviour.CancelAndCancelAwait, cancellationToken, out var token), token);
  21. }
  22. /// <summary>
  23. /// Convert motion handle to UniTask.
  24. /// </summary>
  25. /// <param name="handle">This motion handle</param>
  26. /// <param name="cancelBehaviour">Behavior when canceling</param>
  27. /// <param name="cancellationToken">CancellationToken</param>
  28. /// <returns></returns>
  29. public static UniTask ToUniTask(this MotionHandle handle, CancelBehaviour cancelBehaviour, CancellationToken cancellationToken = default)
  30. {
  31. if (!handle.IsActive()) return UniTask.CompletedTask;
  32. return new UniTask(UniTaskMotionConfiguredSource.Create(handle, cancelBehaviour, cancellationToken, out var token), token);
  33. }
  34. /// <summary>
  35. /// Create a motion data and bind it to AsyncReactiveProperty.
  36. /// </summary>
  37. /// <typeparam name="TValue">The type of value to animate</typeparam>
  38. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  39. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  40. /// <param name="builder">This builder</param>
  41. /// <param name="progress">Target object that implements IProgress</param>
  42. /// <returns>Handle of the created motion data.</returns>
  43. public static MotionHandle BindToAsyncReactiveProperty<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, AsyncReactiveProperty<TValue> reactiveProperty)
  44. where TValue : unmanaged
  45. where TOptions : unmanaged, IMotionOptions
  46. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  47. {
  48. Error.IsNull(reactiveProperty);
  49. return builder.BindWithState(reactiveProperty, static (x, target) =>
  50. {
  51. target.Value = x;
  52. });
  53. }
  54. }
  55. }
  56. #endif