ActivationControlPlayable.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using UnityEngine.Playables;
  2. namespace UnityEngine.Timeline
  3. {
  4. /// <summary>
  5. /// Playable that controls the active state of a GameObject.
  6. /// </summary>
  7. public class ActivationControlPlayable : PlayableBehaviour
  8. {
  9. /// <summary>
  10. /// The state of a GameObject's activeness when a PlayableGraph stops.
  11. /// </summary>
  12. public enum PostPlaybackState
  13. {
  14. /// <summary>
  15. /// Set the GameObject to active when the PlayableGraph stops.
  16. /// </summary>
  17. Active,
  18. /// <summary>
  19. /// Set the GameObject to inactive when the [[PlayableGraph]] stops.
  20. /// </summary>
  21. Inactive,
  22. /// <summary>
  23. /// Revert the GameObject to the active state it was before the [[PlayableGraph]] started.
  24. /// </summary>
  25. Revert
  26. }
  27. enum InitialState
  28. {
  29. Unset,
  30. Active,
  31. Inactive
  32. }
  33. public GameObject gameObject = null;
  34. public PostPlaybackState postPlayback = PostPlaybackState.Revert;
  35. InitialState m_InitialState;
  36. /// <summary>
  37. /// Creates a ScriptPlayable with an ActivationControlPlayable behaviour attached
  38. /// </summary>
  39. /// <param name="graph">PlayableGraph that will own the playable</param>
  40. /// <param name="gameObject">The GameObject that triggered the graph build</param>
  41. /// <param name="postPlaybackState">The state to leave the gameObject after the graph is stopped</param>
  42. /// <returns>Returns a playable that controls activation of a game object</returns>
  43. public static ScriptPlayable<ActivationControlPlayable> Create(PlayableGraph graph, GameObject gameObject, ActivationControlPlayable.PostPlaybackState postPlaybackState)
  44. {
  45. if (gameObject == null)
  46. return ScriptPlayable<ActivationControlPlayable>.Null;
  47. var handle = ScriptPlayable<ActivationControlPlayable>.Create(graph);
  48. var playable = handle.GetBehaviour();
  49. playable.gameObject = gameObject;
  50. playable.postPlayback = postPlaybackState;
  51. return handle;
  52. }
  53. /// <summary>
  54. /// This function is called when the Playable play state is changed to Playables.PlayState.Playing.
  55. /// </summary>
  56. /// <param name="playable">The playable this behaviour is attached to.</param>
  57. /// <param name="info">The information about this frame</param>
  58. public override void OnBehaviourPlay(Playable playable, FrameData info)
  59. {
  60. if (gameObject == null)
  61. return;
  62. gameObject.SetActive(true);
  63. }
  64. /// <summary>
  65. /// This function is called when the Playable play state is changed to PlayState.Paused.
  66. /// </summary>
  67. /// <param name="playable">The playable this behaviour is attached to.</param>
  68. /// <param name="info">The information about this frame</param>
  69. public override void OnBehaviourPause(Playable playable, FrameData info)
  70. {
  71. // OnBehaviourPause can be called if the graph is stopped for a variety of reasons
  72. // the effectivePlayState will test if the pause is due to the clip being out of bounds
  73. if (gameObject != null && info.effectivePlayState == PlayState.Paused)
  74. {
  75. gameObject.SetActive(false);
  76. }
  77. }
  78. /// <summary>
  79. /// This function is called during the ProcessFrame phase of the PlayableGraph.
  80. /// </summary>
  81. /// <param name="playable">The playable this behaviour is attached to.</param>
  82. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  83. /// <param name="userData">unused</param>
  84. public override void ProcessFrame(Playable playable, FrameData info, object userData)
  85. {
  86. if (gameObject != null)// && !gameObject.activeSelf)
  87. gameObject.SetActive(true);
  88. }
  89. /// <summary>
  90. /// This function is called when the PlayableGraph that owns this PlayableBehaviour starts.
  91. /// </summary>
  92. /// <param name="playable">The playable this behaviour is attached to.</param>
  93. public override void OnGraphStart(Playable playable)
  94. {
  95. if (gameObject != null)
  96. {
  97. if (m_InitialState == InitialState.Unset)
  98. m_InitialState = gameObject.activeSelf ? InitialState.Active : InitialState.Inactive;
  99. }
  100. }
  101. /// <summary>
  102. /// This function is called when the Playable that owns the PlayableBehaviour is destroyed.
  103. /// </summary>
  104. /// <param name="playable">The playable this behaviour is attached to.</param>
  105. public override void OnPlayableDestroy(Playable playable)
  106. {
  107. if (gameObject == null || m_InitialState == InitialState.Unset)
  108. return;
  109. switch (postPlayback)
  110. {
  111. case PostPlaybackState.Active:
  112. gameObject.SetActive(true);
  113. break;
  114. case PostPlaybackState.Inactive:
  115. gameObject.SetActive(false);
  116. break;
  117. case PostPlaybackState.Revert:
  118. gameObject.SetActive(m_InitialState == InitialState.Active);
  119. break;
  120. }
  121. }
  122. }
  123. }