LitMotionR3Extensions.cs 2.6 KB

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