LitMotionUniRxExtensions.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #if LITMOTION_SUPPORT_UNIRX
  2. using System;
  3. using UniRx;
  4. namespace LitMotion
  5. {
  6. /// <summary>
  7. /// Provides extension methods for UniRx integration.
  8. /// </summary>
  9. public static class LitMotionUniRxExtensions
  10. {
  11. /// <summary>
  12. /// Create the motion as IObservable.
  13. /// </summary>
  14. /// <typeparam name="TValue">The type of value to animate</typeparam>
  15. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  16. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  17. /// <param name="builder">This builder</param>
  18. /// <returns>Observable of the created motion.</returns>
  19. public static IObservable<TValue> ToObservable<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder)
  20. where TValue : unmanaged
  21. where TOptions : unmanaged, IMotionOptions
  22. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  23. {
  24. var subject = new Subject<TValue>();
  25. builder.SetCallbackData(subject, static (x, subject) => subject.OnNext(x));
  26. builder.buffer.CallbackData.OnCompleteAction += () => subject.OnCompleted();
  27. builder.buffer.CallbackData.OnCancelAction += () => subject.OnCompleted();
  28. var scheduler = builder.buffer.Scheduler;
  29. builder.SetMotionData();
  30. builder.Schedule(scheduler, ref builder.buffer.Data, ref builder.buffer.CallbackData);
  31. return subject;
  32. }
  33. /// <summary>
  34. /// Create a motion data and bind it to ReactiveProperty.
  35. /// </summary>
  36. /// <typeparam name="TValue">The type of value to animate</typeparam>
  37. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  38. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  39. /// <param name="builder">This builder</param>
  40. /// <param name="progress">Target object that implements IProgress</param>
  41. /// <returns>Handle of the created motion data.</returns>
  42. public static MotionHandle BindToReactiveProperty<TValue, TOptions, TAdapter>(this MotionBuilder<TValue, TOptions, TAdapter> builder, ReactiveProperty<TValue> reactiveProperty)
  43. where TValue : unmanaged
  44. where TOptions : unmanaged, IMotionOptions
  45. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  46. {
  47. Error.IsNull(reactiveProperty);
  48. return builder.BindWithState(reactiveProperty, static (x, target) =>
  49. {
  50. target.Value = x;
  51. });
  52. }
  53. }
  54. }
  55. #endif