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