TimeReferenceUtility.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine.Timeline;
  2. namespace UnityEditor.Timeline
  3. {
  4. static class TimeReferenceUtility
  5. {
  6. static WindowState state { get { return TimelineWindow.instance.state; } }
  7. public static double SnapToFrame(double time)
  8. {
  9. if (state.timeReferenceMode == TimeReferenceMode.Global)
  10. {
  11. time = state.editSequence.ToGlobalTime(time);
  12. time = TimeUtility.RoundToFrame(time, state.referenceSequence.frameRate);
  13. return state.editSequence.ToLocalTime(time);
  14. }
  15. return TimeUtility.RoundToFrame(time, state.referenceSequence.frameRate);
  16. }
  17. public static string ToTimeString(double time, string format = "F2")
  18. {
  19. if (state.timeReferenceMode == TimeReferenceMode.Global)
  20. time = state.editSequence.ToGlobalTime(time);
  21. return state.editSequence.viewModel.timeInFrames ?
  22. TimeUtility.TimeAsFrames(time, state.referenceSequence.frameRate, format) :
  23. TimeUtility.TimeAsTimeCode(time, state.referenceSequence.frameRate, format);
  24. }
  25. public static double FromTimeString(string timeString)
  26. {
  27. double newTime;
  28. if (state.timeInFrames)
  29. {
  30. double newFrameDouble;
  31. if (double.TryParse(timeString, out newFrameDouble))
  32. newTime = TimeUtility.FromFrames(newFrameDouble, state.referenceSequence.frameRate);
  33. else
  34. newTime = state.editSequence.time;
  35. }
  36. else
  37. {
  38. newTime = TimeUtility.ParseTimeCode(timeString, state.referenceSequence.frameRate, -1);
  39. }
  40. if (newTime >= 0.0)
  41. {
  42. return state.timeReferenceMode == TimeReferenceMode.Global ?
  43. state.editSequence.ToLocalTime(newTime) : newTime;
  44. }
  45. return state.editSequence.time;
  46. }
  47. }
  48. }