ActivationTrack.cs 2.8 KB

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