DirectorControlPlayable.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. namespace UnityEngine.Timeline
  5. {
  6. /// <summary>
  7. /// Playable Behaviour used to control a PlayableDirector.
  8. /// </summary>
  9. /// <remarks>
  10. /// This playable is used to control other PlayableDirector components from a Timeline sequence.
  11. /// </remarks>
  12. public class DirectorControlPlayable : PlayableBehaviour
  13. {
  14. /// <summary>
  15. /// The PlayableDirector being controlled by this PlayableBehaviour
  16. /// </summary>
  17. public PlayableDirector director;
  18. private bool m_SyncTime = false;
  19. private double m_AssetDuration = double.MaxValue;
  20. /// <summary>
  21. /// Creates a Playable with a DirectorControlPlayable attached
  22. /// </summary>
  23. /// <param name="graph">The graph to inject the playable into</param>
  24. /// <param name="director">The director to control</param>
  25. /// <returns>Returns a Playable with a DirectorControlPlayable attached</returns>
  26. public static ScriptPlayable<DirectorControlPlayable> Create(PlayableGraph graph, PlayableDirector director)
  27. {
  28. if (director == null)
  29. return ScriptPlayable<DirectorControlPlayable>.Null;
  30. var handle = ScriptPlayable<DirectorControlPlayable>.Create(graph);
  31. handle.GetBehaviour().director = director;
  32. #if UNITY_EDITOR
  33. if (!Application.isPlaying && UnityEditor.PrefabUtility.IsPartOfPrefabInstance(director))
  34. UnityEditor.PrefabUtility.prefabInstanceUpdated += handle.GetBehaviour().OnPrefabUpdated;
  35. #endif
  36. return handle;
  37. }
  38. /// <summary>
  39. /// This function is called when this PlayableBehaviour is destroyed.
  40. /// </summary>
  41. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  42. public override void OnPlayableDestroy(Playable playable)
  43. {
  44. #if UNITY_EDITOR
  45. if (!Application.isPlaying)
  46. UnityEditor.PrefabUtility.prefabInstanceUpdated -= OnPrefabUpdated;
  47. #endif
  48. if (director != null && director.playableAsset != null)
  49. director.Stop();
  50. }
  51. /// <summary>
  52. /// This function is called during the PrepareFrame phase of the PlayableGraph.
  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 PrepareFrame(Playable playable, FrameData info)
  57. {
  58. if (director == null || !director.isActiveAndEnabled || director.playableAsset == null)
  59. return;
  60. // resync the time on an evaluate or a time jump (caused by loops, or some setTime calls)
  61. m_SyncTime |= (info.evaluationType == FrameData.EvaluationType.Evaluate) ||
  62. DetectDiscontinuity(playable, info);
  63. SyncSpeed(info.effectiveSpeed);
  64. SyncPlayState(playable.GetGraph(), playable.GetTime());
  65. }
  66. /// <summary>
  67. /// This function is called when the Playable play state is changed to Playables.PlayState.Playing.
  68. /// </summary>
  69. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  70. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  71. public override void OnBehaviourPlay(Playable playable, FrameData info)
  72. {
  73. m_SyncTime = true;
  74. if (director != null && director.playableAsset != null)
  75. m_AssetDuration = director.playableAsset.duration;
  76. }
  77. /// <summary>
  78. /// This function is called when the Playable play state is changed to PlayState.Paused.
  79. /// </summary>
  80. /// <param name="playable">The playable this behaviour is attached to.</param>
  81. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  82. public override void OnBehaviourPause(Playable playable, FrameData info)
  83. {
  84. if (director != null && director.playableAsset != null)
  85. {
  86. if (info.effectivePlayState == PlayState.Playing) // graph was paused
  87. director.Pause();
  88. else
  89. director.Stop();
  90. }
  91. }
  92. /// <summary>
  93. /// This function is called during the ProcessFrame phase of the PlayableGraph.
  94. /// </summary>
  95. /// <param name="playable">The playable this behaviour is attached to.</param>
  96. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  97. /// <param name="playerData">unused</param>
  98. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  99. {
  100. if (director == null || !director.isActiveAndEnabled || director.playableAsset == null)
  101. return;
  102. if (m_SyncTime || DetectOutOfSync(playable))
  103. {
  104. UpdateTime(playable);
  105. director.Evaluate();
  106. }
  107. m_SyncTime = false;
  108. }
  109. #if UNITY_EDITOR
  110. void OnPrefabUpdated(GameObject go)
  111. {
  112. // When the prefab asset is updated, we rebuild the graph to reflect the changes in editor
  113. if (UnityEditor.PrefabUtility.GetRootGameObject(director) == go)
  114. director.RebuildGraph();
  115. }
  116. #endif
  117. void SyncSpeed(double speed)
  118. {
  119. if (director.playableGraph.IsValid())
  120. {
  121. int roots = director.playableGraph.GetRootPlayableCount();
  122. for (int i = 0; i < roots; i++)
  123. {
  124. var rootPlayable = director.playableGraph.GetRootPlayable(i);
  125. if (rootPlayable.IsValid())
  126. {
  127. rootPlayable.SetSpeed(speed);
  128. }
  129. }
  130. }
  131. }
  132. void SyncPlayState(PlayableGraph graph, double playableTime)
  133. {
  134. bool expectedFinished = (playableTime >= m_AssetDuration) && director.extrapolationMode == DirectorWrapMode.None;
  135. if (graph.IsPlaying() && !expectedFinished)
  136. director.Play();
  137. else
  138. director.Pause();
  139. }
  140. bool DetectDiscontinuity(Playable playable, FrameData info)
  141. {
  142. return Math.Abs(playable.GetTime() - playable.GetPreviousTime() - info.m_DeltaTime * info.m_EffectiveSpeed) > DiscreteTime.tickValue;
  143. }
  144. bool DetectOutOfSync(Playable playable)
  145. {
  146. double expectedTime = playable.GetTime();
  147. if (playable.GetTime() >= m_AssetDuration)
  148. {
  149. if (director.extrapolationMode == DirectorWrapMode.None)
  150. return false;
  151. else if (director.extrapolationMode == DirectorWrapMode.Hold)
  152. expectedTime = m_AssetDuration;
  153. else if (m_AssetDuration > float.Epsilon) // loop
  154. expectedTime = expectedTime % m_AssetDuration;
  155. }
  156. if (!Mathf.Approximately((float)expectedTime, (float)director.time))
  157. {
  158. #if UNITY_EDITOR
  159. double lastDelta = playable.GetTime() - playable.GetPreviousTime();
  160. if (UnityEditor.Unsupported.IsDeveloperBuild())
  161. Debug.LogWarningFormat("Internal Warning - Control track desync detected on {2} ({0:F10} vs {1:F10} with delta {3:F10}). Time will be resynchronized. Known to happen with nested control tracks", playable.GetTime(), director.time, director.name, lastDelta);
  162. #endif
  163. return true;
  164. }
  165. return false;
  166. }
  167. // We need to handle loop modes explicitly since we are setting the time directly
  168. void UpdateTime(Playable playable)
  169. {
  170. double duration = Math.Max(0.1, director.playableAsset.duration);
  171. switch (director.extrapolationMode)
  172. {
  173. case DirectorWrapMode.Hold:
  174. director.time = Math.Min(duration, Math.Max(0, playable.GetTime()));
  175. break;
  176. case DirectorWrapMode.Loop:
  177. director.time = Math.Max(0, playable.GetTime() % duration);
  178. break;
  179. case DirectorWrapMode.None:
  180. director.time = playable.GetTime();
  181. break;
  182. }
  183. }
  184. }
  185. }