暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DirectorControlPlayable.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. bool m_SyncTime = false;
  19. 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. SyncStart(playable.GetGraph(), playable.GetTime());
  65. #if !UNITY_2021_2_OR_NEWER
  66. SyncStop(playable.GetGraph(), playable.GetTime());
  67. #endif
  68. }
  69. /// <summary>
  70. /// This function is called when the Playable play state is changed to Playables.PlayState.Playing.
  71. /// </summary>
  72. /// <param name="playable">The Playable that owns the current PlayableBehaviour.</param>
  73. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  74. public override void OnBehaviourPlay(Playable playable, FrameData info)
  75. {
  76. m_SyncTime = true;
  77. if (director != null && director.playableAsset != null)
  78. m_AssetDuration = director.playableAsset.duration;
  79. }
  80. /// <summary>
  81. /// This function is called when the Playable play state is changed to PlayState.Paused.
  82. /// </summary>
  83. /// <param name="playable">The playable this behaviour is attached to.</param>
  84. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  85. public override void OnBehaviourPause(Playable playable, FrameData info)
  86. {
  87. if (director != null && director.playableAsset != null)
  88. {
  89. if (info.effectivePlayState == PlayState.Playing) // graph was paused
  90. director.Pause();
  91. else
  92. director.Stop();
  93. }
  94. }
  95. /// <summary>
  96. /// This function is called during the ProcessFrame phase of the PlayableGraph.
  97. /// </summary>
  98. /// <param name="playable">The playable this behaviour is attached to.</param>
  99. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  100. /// <param name="playerData">unused</param>
  101. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  102. {
  103. if (director == null || !director.isActiveAndEnabled || director.playableAsset == null)
  104. return;
  105. if (m_SyncTime || DetectOutOfSync(playable))
  106. {
  107. UpdateTime(playable);
  108. if (director.playableGraph.IsValid())
  109. {
  110. director.playableGraph.Evaluate();
  111. #if TIMELINE_FRAMEACCURATE
  112. director.playableGraph.SynchronizeEvaluation(playable.GetGraph());
  113. #endif
  114. }
  115. else
  116. {
  117. director.Evaluate();
  118. }
  119. }
  120. m_SyncTime = false;
  121. #if UNITY_2021_2_OR_NEWER
  122. SyncStop(playable.GetGraph(), playable.GetTime());
  123. #endif
  124. }
  125. #if UNITY_EDITOR
  126. void OnPrefabUpdated(GameObject go)
  127. {
  128. // When the prefab asset is updated, we rebuild the graph to reflect the changes in editor
  129. if (UnityEditor.PrefabUtility.GetRootGameObject(director) == go)
  130. director.RebuildGraph();
  131. }
  132. #endif
  133. void SyncSpeed(double speed)
  134. {
  135. if (director.playableGraph.IsValid())
  136. {
  137. int roots = director.playableGraph.GetRootPlayableCount();
  138. for (int i = 0; i < roots; i++)
  139. {
  140. var rootPlayable = director.playableGraph.GetRootPlayable(i);
  141. if (rootPlayable.IsValid())
  142. {
  143. rootPlayable.SetSpeed(speed);
  144. }
  145. }
  146. }
  147. }
  148. void SyncStart(PlayableGraph graph, double time)
  149. {
  150. if (director.state == PlayState.Playing
  151. || !graph.IsPlaying()
  152. || (director.extrapolationMode == DirectorWrapMode.None && time > m_AssetDuration))
  153. return;
  154. #if TIMELINE_FRAMEACCURATE
  155. if (graph.IsMatchFrameRateEnabled())
  156. director.Play(graph.GetFrameRate());
  157. else
  158. director.Play();
  159. #else
  160. director.Play();
  161. #endif
  162. }
  163. void SyncStop(PlayableGraph graph, double time)
  164. {
  165. if (director.state == PlayState.Paused
  166. || (graph.IsPlaying() && (director.extrapolationMode != DirectorWrapMode.None || time < m_AssetDuration)))
  167. return;
  168. if (director.state == PlayState.Paused)
  169. return;
  170. bool expectedFinished = director.extrapolationMode == DirectorWrapMode.None && time > m_AssetDuration;
  171. if (expectedFinished || !graph.IsPlaying())
  172. director.Pause();
  173. }
  174. bool DetectDiscontinuity(Playable playable, FrameData info)
  175. {
  176. return Math.Abs(playable.GetTime() - playable.GetPreviousTime() - info.m_DeltaTime * info.m_EffectiveSpeed) > DiscreteTime.tickValue;
  177. }
  178. bool DetectOutOfSync(Playable playable)
  179. {
  180. double expectedTime = playable.GetTime();
  181. if (playable.GetTime() >= m_AssetDuration)
  182. {
  183. switch (director.extrapolationMode)
  184. {
  185. case DirectorWrapMode.None:
  186. expectedTime = m_AssetDuration;
  187. break;
  188. case DirectorWrapMode.Hold:
  189. expectedTime = m_AssetDuration;
  190. break;
  191. case DirectorWrapMode.Loop:
  192. expectedTime %= m_AssetDuration;
  193. break;
  194. }
  195. }
  196. if (!Mathf.Approximately((float)expectedTime, (float)director.time))
  197. {
  198. #if UNITY_EDITOR
  199. double lastDelta = playable.GetTime() - playable.GetPreviousTime();
  200. if (UnityEditor.Unsupported.IsDeveloperBuild())
  201. 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);
  202. #endif
  203. return true;
  204. }
  205. return false;
  206. }
  207. // We need to handle loop modes explicitly since we are setting the time directly
  208. void UpdateTime(Playable playable)
  209. {
  210. double duration = Math.Max(0.1, director.playableAsset.duration);
  211. switch (director.extrapolationMode)
  212. {
  213. case DirectorWrapMode.Hold:
  214. director.time = Math.Min(duration, Math.Max(0, playable.GetTime()));
  215. break;
  216. case DirectorWrapMode.Loop:
  217. director.time = Math.Max(0, playable.GetTime() % duration);
  218. break;
  219. case DirectorWrapMode.None:
  220. director.time = Math.Min(duration, Math.Max(0, playable.GetTime()));
  221. break;
  222. }
  223. }
  224. }
  225. }