TimeControlPlayable.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine.Playables;
  2. namespace UnityEngine.Timeline
  3. {
  4. /// <summary>
  5. /// A PlayableBehaviour that manages a component that implements the ITimeControl interface
  6. /// </summary>
  7. public class TimeControlPlayable : PlayableBehaviour
  8. {
  9. ITimeControl m_timeControl;
  10. bool m_started;
  11. /// <summary>
  12. /// Creates a Playable with a TimeControlPlayable behaviour attached
  13. /// </summary>
  14. /// <param name="graph">The PlayableGraph to inject the Playable into.</param>
  15. /// <param name="timeControl"></param>
  16. /// <returns></returns>
  17. public static ScriptPlayable<TimeControlPlayable> Create(PlayableGraph graph, ITimeControl timeControl)
  18. {
  19. if (timeControl == null)
  20. return ScriptPlayable<TimeControlPlayable>.Null;
  21. var handle = ScriptPlayable<TimeControlPlayable>.Create(graph);
  22. handle.GetBehaviour().Initialize(timeControl);
  23. return handle;
  24. }
  25. /// <summary>
  26. /// Initializes the behaviour
  27. /// </summary>
  28. /// <param name="timeControl">Component that implements the ITimeControl interface</param>
  29. public void Initialize(ITimeControl timeControl)
  30. {
  31. m_timeControl = timeControl;
  32. }
  33. /// <summary>
  34. /// This function is called during the PrepareFrame phase of the PlayableGraph.
  35. /// </summary>
  36. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  37. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  38. public override void PrepareFrame(Playable playable, FrameData info)
  39. {
  40. Debug.Assert(m_started, "PrepareFrame has been called without OnControlTimeStart being called first.");
  41. if (m_timeControl != null)
  42. m_timeControl.SetTime(playable.GetTime());
  43. }
  44. /// <summary>
  45. /// This function is called when the Playable play state is changed to Playables.PlayState.Playing.
  46. /// </summary>
  47. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  48. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  49. public override void OnBehaviourPlay(Playable playable, FrameData info)
  50. {
  51. if (m_timeControl == null)
  52. return;
  53. if (!m_started)
  54. {
  55. m_timeControl.OnControlTimeStart();
  56. m_started = true;
  57. }
  58. }
  59. /// <summary>
  60. /// This function is called when the Playable play state is changed to PlayState.Paused.
  61. /// </summary>
  62. /// <param name="playable">The playable this behaviour is attached to.</param>
  63. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  64. public override void OnBehaviourPause(Playable playable, FrameData info)
  65. {
  66. if (m_timeControl == null)
  67. return;
  68. if (m_started)
  69. {
  70. m_timeControl.OnControlTimeStop();
  71. m_started = false;
  72. }
  73. }
  74. }
  75. }