TimelineWindow_PreviewPlayMode.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. partial class TimelineWindow
  8. {
  9. void OnPreviewPlayModeChanged(bool isPlaying)
  10. {
  11. if (state != null && !state.ignorePreview && isPlaying)
  12. {
  13. PreparePreviewPlay();
  14. EditorApplication.update += OnPreviewPlay;
  15. }
  16. else
  17. {
  18. EditorApplication.update -= OnPreviewPlay;
  19. }
  20. }
  21. void PreparePreviewPlay()
  22. {
  23. if (state == null || state.masterSequence.asset == null || state.masterSequence.director == null)
  24. return;
  25. if (state.playRangeEnabled && !state.isJogging)
  26. {
  27. EnsurePlayRangeIsRespected();
  28. }
  29. }
  30. internal void OnPreviewPlay()
  31. {
  32. if (state == null || state.masterSequence.asset == null || state.masterSequence.director == null)
  33. return;
  34. var director = state.masterSequence.director;
  35. if (director.timeUpdateMode == DirectorUpdateMode.Manual)
  36. {
  37. Repaint();
  38. return;
  39. }
  40. if (state.isJogging)
  41. {
  42. ApplyJog();
  43. }
  44. else if (state.playRangeEnabled)
  45. {
  46. EnsurePlayRangeIsRespected();
  47. }
  48. if (director.extrapolationMode == DirectorWrapMode.None && director.playableGraph.IsValid() && !state.playRangeEnabled && director.playableGraph.IsDone())
  49. {
  50. //reset time if we hit the end of the timeline
  51. state.masterSequence.time = 0.0;
  52. state.Pause();
  53. }
  54. Repaint();
  55. AudioMixerWindow.RepaintAudioMixerWindow();
  56. }
  57. void ApplyJog()
  58. {
  59. state.masterSequence.time = Math.Max(0.0, Math.Min(state.masterSequence.duration, state.masterSequence.time + state.playbackSpeed));
  60. }
  61. void EnsurePlayRangeIsRespected()
  62. {
  63. var playRangeTime = state.playRange;
  64. var time = state.masterSequence.time;
  65. if (Math.Abs(time - playRangeTime.y) < TimeUtility.kFrameRateEpsilon || time > playRangeTime.y || time < playRangeTime.x)
  66. {
  67. state.masterSequence.time = playRangeTime.x;
  68. // case 1215926 : Special case to make the director mode to play if the wrap mode is None.
  69. // In that mode, the engine stop the graph before we can ensure play range is respected.
  70. if (!state.playing && state.masterSequence.director.extrapolationMode == DirectorWrapMode.None)
  71. state.Play();
  72. }
  73. }
  74. }
  75. }