DirectorControlPlayable.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. public override void OnPlayableDestroy(Playable playable)
  39. {
  40. #if UNITY_EDITOR
  41. if (!Application.isPlaying)
  42. UnityEditor.PrefabUtility.prefabInstanceUpdated -= OnPrefabUpdated;
  43. #endif
  44. if (director != null && director.playableAsset != null)
  45. director.Stop();
  46. }
  47. /// <summary>
  48. /// This function is called during the PrepareFrame phase of the PlayableGraph.
  49. /// </summary>
  50. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  51. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  52. public override void PrepareFrame(Playable playable, FrameData info)
  53. {
  54. if (director == null || !director.isActiveAndEnabled || director.playableAsset == null)
  55. return;
  56. // resync the time on an evaluate or a time jump (caused by loops, or some setTime calls)
  57. m_SyncTime |= (info.evaluationType == FrameData.EvaluationType.Evaluate) ||
  58. DetectDiscontinuity(playable, info);
  59. SyncSpeed(info.effectiveSpeed);
  60. SyncPlayState(playable.GetGraph(), playable.GetTime());
  61. }
  62. /// <summary>
  63. /// This function is called when the Playable play state is changed to Playables.PlayState.Playing.
  64. /// </summary>
  65. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  66. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  67. public override void OnBehaviourPlay(Playable playable, FrameData info)
  68. {
  69. m_SyncTime = true;
  70. if (director != null && director.playableAsset != null)
  71. m_AssetDuration = director.playableAsset.duration;
  72. }
  73. /// <summary>
  74. /// This function is called when the Playable play state is changed to PlayState.Paused.
  75. /// </summary>
  76. /// <param name="playable">The playable this behaviour is attached to.</param>
  77. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  78. public override void OnBehaviourPause(Playable playable, FrameData info)
  79. {
  80. if (director != null && director.playableAsset != null)
  81. {
  82. if (info.effectivePlayState == PlayState.Playing) // graph was paused
  83. director.Pause();
  84. else
  85. director.Stop();
  86. }
  87. }
  88. /// <summary>
  89. /// This function is called during the ProcessFrame phase of the PlayableGraph.
  90. /// </summary>
  91. /// <param name="playable">The playable this behaviour is attached to.</param>
  92. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  93. /// <param name="playerData">unused</param>
  94. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  95. {
  96. if (director == null || !director.isActiveAndEnabled || director.playableAsset == null)
  97. return;
  98. if (m_SyncTime || DetectOutOfSync(playable))
  99. {
  100. UpdateTime(playable);
  101. director.Evaluate();
  102. }
  103. m_SyncTime = false;
  104. }
  105. #if UNITY_EDITOR
  106. void OnPrefabUpdated(GameObject go)
  107. {
  108. // When the prefab asset is updated, we rebuild the graph to reflect the changes in editor
  109. if (UnityEditor.PrefabUtility.GetRootGameObject(director) == go)
  110. director.RebuildGraph();
  111. }
  112. #endif
  113. void SyncSpeed(double speed)
  114. {
  115. if (director.playableGraph.IsValid())
  116. {
  117. int roots = director.playableGraph.GetRootPlayableCount();
  118. for (int i = 0; i < roots; i++)
  119. {
  120. var rootPlayable = director.playableGraph.GetRootPlayable(i);
  121. if (rootPlayable.IsValid())
  122. {
  123. rootPlayable.SetSpeed(speed);
  124. }
  125. }
  126. }
  127. }
  128. void SyncPlayState(PlayableGraph graph, double playableTime)
  129. {
  130. bool expectedFinished = (playableTime >= m_AssetDuration) && director.extrapolationMode == DirectorWrapMode.None;
  131. if (graph.IsPlaying() && !expectedFinished)
  132. director.Play();
  133. else
  134. director.Pause();
  135. }
  136. bool DetectDiscontinuity(Playable playable, FrameData info)
  137. {
  138. return Math.Abs(playable.GetTime() - playable.GetPreviousTime() - info.m_DeltaTime * info.m_EffectiveSpeed) > DiscreteTime.tickValue;
  139. }
  140. bool DetectOutOfSync(Playable playable)
  141. {
  142. double expectedTime = playable.GetTime();
  143. if (playable.GetTime() >= m_AssetDuration)
  144. {
  145. if (director.extrapolationMode == DirectorWrapMode.None)
  146. return false;
  147. else if (director.extrapolationMode == DirectorWrapMode.Hold)
  148. expectedTime = m_AssetDuration;
  149. else if (m_AssetDuration > float.Epsilon) // loop
  150. expectedTime = expectedTime % m_AssetDuration;
  151. }
  152. if (!Mathf.Approximately((float)expectedTime, (float)director.time))
  153. {
  154. #if UNITY_EDITOR
  155. double lastDelta = playable.GetTime() - playable.GetPreviousTime();
  156. if (UnityEditor.Unsupported.IsDeveloperBuild())
  157. 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);
  158. #endif
  159. return true;
  160. }
  161. return false;
  162. }
  163. // We need to handle loop modes explicitly since we are setting the time directly
  164. void UpdateTime(Playable playable)
  165. {
  166. double duration = Math.Max(0.1, director.playableAsset.duration);
  167. switch (director.extrapolationMode)
  168. {
  169. case DirectorWrapMode.Hold:
  170. director.time = Math.Min(duration, Math.Max(0, playable.GetTime()));
  171. break;
  172. case DirectorWrapMode.Loop:
  173. director.time = Math.Max(0, playable.GetTime() % duration);
  174. break;
  175. case DirectorWrapMode.None:
  176. director.time = playable.GetTime();
  177. break;
  178. }
  179. }
  180. }
  181. }