Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ActivationControlPlayable.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /// <summary>
  34. /// The GameObject to control.
  35. /// </summary>
  36. public GameObject gameObject = null;
  37. /// <inheritdoc cref="ActivationControlPlayable.PostPlaybackState"/>
  38. public PostPlaybackState postPlayback = PostPlaybackState.Revert;
  39. InitialState m_InitialState;
  40. /// <summary>
  41. /// Creates a ScriptPlayable with an ActivationControlPlayable behaviour attached
  42. /// </summary>
  43. /// <param name="graph">PlayableGraph that will own the playable</param>
  44. /// <param name="gameObject">The GameObject that triggered the graph build</param>
  45. /// <param name="postPlaybackState">The state to leave the gameObject after the graph is stopped</param>
  46. /// <returns>Returns a playable that controls activation of a game object</returns>
  47. public static ScriptPlayable<ActivationControlPlayable> Create(PlayableGraph graph, GameObject gameObject, ActivationControlPlayable.PostPlaybackState postPlaybackState)
  48. {
  49. if (gameObject == null)
  50. return ScriptPlayable<ActivationControlPlayable>.Null;
  51. var handle = ScriptPlayable<ActivationControlPlayable>.Create(graph);
  52. var playable = handle.GetBehaviour();
  53. playable.gameObject = gameObject;
  54. playable.postPlayback = postPlaybackState;
  55. return handle;
  56. }
  57. /// <summary>
  58. /// This function is called when the Playable play state is changed to Playables.PlayState.Playing.
  59. /// </summary>
  60. /// <param name="playable">The playable this behaviour is attached to.</param>
  61. /// <param name="info">The information about this frame</param>
  62. public override void OnBehaviourPlay(Playable playable, FrameData info)
  63. {
  64. if (gameObject == null)
  65. return;
  66. gameObject.SetActive(true);
  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">The information about this frame</param>
  73. public override void OnBehaviourPause(Playable playable, FrameData info)
  74. {
  75. // OnBehaviourPause can be called if the graph is stopped for a variety of reasons
  76. // the effectivePlayState will test if the pause is due to the clip being out of bounds
  77. if (gameObject != null && info.effectivePlayState == PlayState.Paused)
  78. {
  79. gameObject.SetActive(false);
  80. }
  81. }
  82. /// <summary>
  83. /// This function is called during the ProcessFrame phase of the PlayableGraph.
  84. /// </summary>
  85. /// <param name="playable">The playable this behaviour is attached to.</param>
  86. /// <param name="info">A FrameData structure that contains information about the current frame context.</param>
  87. /// <param name="userData">unused</param>
  88. public override void ProcessFrame(Playable playable, FrameData info, object userData)
  89. {
  90. if (gameObject != null)// && !gameObject.activeSelf)
  91. gameObject.SetActive(true);
  92. }
  93. /// <summary>
  94. /// This function is called when the PlayableGraph that owns this PlayableBehaviour starts.
  95. /// </summary>
  96. /// <param name="playable">The playable this behaviour is attached to.</param>
  97. public override void OnGraphStart(Playable playable)
  98. {
  99. if (gameObject != null)
  100. {
  101. if (m_InitialState == InitialState.Unset)
  102. m_InitialState = gameObject.activeSelf ? InitialState.Active : InitialState.Inactive;
  103. }
  104. }
  105. /// <summary>
  106. /// This function is called when the Playable that owns the PlayableBehaviour is destroyed.
  107. /// </summary>
  108. /// <param name="playable">The playable this behaviour is attached to.</param>
  109. public override void OnPlayableDestroy(Playable playable)
  110. {
  111. if (gameObject == null || m_InitialState == InitialState.Unset)
  112. return;
  113. switch (postPlayback)
  114. {
  115. case PostPlaybackState.Active:
  116. gameObject.SetActive(true);
  117. break;
  118. case PostPlaybackState.Inactive:
  119. gameObject.SetActive(false);
  120. break;
  121. case PostPlaybackState.Revert:
  122. gameObject.SetActive(m_InitialState == InitialState.Active);
  123. break;
  124. }
  125. }
  126. }
  127. }