ActivationTrack.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using UnityEngine.Playables;
  3. namespace UnityEngine.Timeline
  4. {
  5. /// <summary>
  6. /// Track that can be used to control the active state of a GameObject.
  7. /// </summary>
  8. [Serializable]
  9. [TrackClipType(typeof(ActivationPlayableAsset))]
  10. [TrackBindingType(typeof(GameObject))]
  11. public class ActivationTrack : TrackAsset
  12. {
  13. [SerializeField]
  14. PostPlaybackState m_PostPlaybackState = PostPlaybackState.LeaveAsIs;
  15. ActivationMixerPlayable m_ActivationMixer;
  16. /// <summary>
  17. /// Specify what state to leave the GameObject in after the Timeline has finished playing.
  18. /// </summary>
  19. public enum PostPlaybackState
  20. {
  21. /// <summary>
  22. /// Set the GameObject to active.
  23. /// </summary>
  24. Active,
  25. /// <summary>
  26. /// Set the GameObject to Inactive.
  27. /// </summary>
  28. Inactive,
  29. /// <summary>
  30. /// Revert the GameObject to the state in was in before the Timeline was playing.
  31. /// </summary>
  32. Revert,
  33. /// <summary>
  34. /// Leave the GameObject in the state it was when the Timeline was stopped.
  35. /// </summary>
  36. LeaveAsIs
  37. }
  38. internal override bool CanCompileClips()
  39. {
  40. return !hasClips || base.CanCompileClips();
  41. }
  42. /// <summary>
  43. /// Specifies what state to leave the GameObject in after the Timeline has finished playing.
  44. /// </summary>
  45. public PostPlaybackState postPlaybackState
  46. {
  47. get { return m_PostPlaybackState; }
  48. set { m_PostPlaybackState = value; UpdateTrackMode(); }
  49. }
  50. /// <inheritdoc/>
  51. public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
  52. {
  53. var mixer = ActivationMixerPlayable.Create(graph, inputCount);
  54. m_ActivationMixer = mixer.GetBehaviour();
  55. UpdateTrackMode();
  56. return mixer;
  57. }
  58. internal void UpdateTrackMode()
  59. {
  60. if (m_ActivationMixer != null)
  61. m_ActivationMixer.postPlaybackState = m_PostPlaybackState;
  62. }
  63. /// <inheritdoc/>
  64. public override void GatherProperties(PlayableDirector director, IPropertyCollector driver)
  65. {
  66. var gameObject = GetGameObjectBinding(director);
  67. if (gameObject != null)
  68. {
  69. driver.AddFromName(gameObject, "m_IsActive");
  70. }
  71. }
  72. /// <inheritdoc/>
  73. protected override void OnCreateClip(TimelineClip clip)
  74. {
  75. clip.displayName = "Active";
  76. base.OnCreateClip(clip);
  77. }
  78. }
  79. }