PlayerLoopMotionScheduler.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using UnityTime = UnityEngine.Time;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. namespace LitMotion
  7. {
  8. internal sealed class PlayerLoopMotionScheduler : IMotionScheduler
  9. {
  10. public readonly PlayerLoopTiming playerLoopTiming;
  11. public readonly MotionTimeKind timeKind;
  12. internal PlayerLoopMotionScheduler(PlayerLoopTiming playerLoopTiming, MotionTimeKind timeKind)
  13. {
  14. this.playerLoopTiming = playerLoopTiming;
  15. this.timeKind = timeKind;
  16. }
  17. public double Time
  18. {
  19. get
  20. {
  21. if (playerLoopTiming == PlayerLoopTiming.FixedUpdate)
  22. {
  23. return timeKind switch
  24. {
  25. MotionTimeKind.Time => UnityTime.fixedTimeAsDouble,
  26. MotionTimeKind.UnscaledTime => UnityTime.fixedUnscaledTimeAsDouble,
  27. MotionTimeKind.Realtime => UnityTime.realtimeSinceStartupAsDouble,
  28. _ => throw new NotSupportedException("Invalid TimeKind")
  29. };
  30. }
  31. return timeKind switch
  32. {
  33. MotionTimeKind.Time => UnityTime.timeAsDouble,
  34. MotionTimeKind.UnscaledTime => UnityTime.unscaledTimeAsDouble,
  35. MotionTimeKind.Realtime => UnityTime.realtimeSinceStartupAsDouble,
  36. _ => throw new NotSupportedException("Invalid TimeKind")
  37. };
  38. }
  39. }
  40. public MotionHandle Schedule<TValue, TOptions, TAdapter>(ref MotionData<TValue, TOptions> data, ref MotionCallbackData callbackData)
  41. where TValue : unmanaged
  42. where TOptions : unmanaged, IMotionOptions
  43. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  44. {
  45. data.Core.TimeKind = timeKind;
  46. #if UNITY_EDITOR
  47. if (EditorApplication.isPlaying)
  48. {
  49. return MotionDispatcher.Schedule<TValue, TOptions, TAdapter>(data, callbackData, playerLoopTiming);
  50. }
  51. else
  52. {
  53. return EditorMotionDispatcher.Schedule<TValue, TOptions, TAdapter>(data, callbackData);
  54. }
  55. #else
  56. return MotionDispatcher.Schedule<TValue, TOptions, TAdapter>(data, callbackData, playerLoopTiming);
  57. #endif
  58. }
  59. }
  60. }