TimelineEditor.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine.Playables;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline
  7. {
  8. /// <summary>
  9. /// Information currently being edited in the Timeline Editor Window.
  10. /// </summary>
  11. public static class TimelineEditor
  12. {
  13. /// <summary>
  14. /// The PlayableDirector associated with the timeline currently being shown in the Timeline window.
  15. /// </summary>
  16. public static PlayableDirector inspectedDirector { get { return state == null ? null : state.editSequence.director; } }
  17. /// <summary>
  18. /// The PlayableDirector responsible for the playback of the timeline currently being shown in the Timeline window.
  19. /// </summary>
  20. public static PlayableDirector masterDirector { get { return state == null ? null : state.masterSequence.director; } }
  21. /// <summary>
  22. /// The TimelineAsset currently being shown in the Timeline window.
  23. /// </summary>
  24. public static TimelineAsset inspectedAsset { get { return state == null ? null : state.editSequence.asset; } }
  25. /// <summary>
  26. /// The TimelineAsset at the root of the hierarchy currently being shown in the Timeline window.
  27. /// </summary>
  28. public static TimelineAsset masterAsset { get { return state == null ? null : state.masterSequence.asset; } }
  29. /// <summary>
  30. /// The PlayableDirector currently being shown in the Timeline Editor Window.
  31. /// </summary>
  32. [Obsolete("playableDirector is ambiguous. Please select either inspectedDirector or masterDirector instead.", false)]
  33. public static PlayableDirector playableDirector { get { return inspectedDirector; } }
  34. /// <summary>
  35. /// The TimelineAsset currently being shown in the Timeline Editor Window.
  36. /// </summary>
  37. [Obsolete("timelineAsset is ambiguous. Please select either inspectedAsset or masterAsset instead.", false)]
  38. public static TimelineAsset timelineAsset { get { return inspectedAsset; } }
  39. /// <summary>
  40. /// <para>
  41. /// Refreshes the different components affected by the currently inspected
  42. /// <see cref="UnityEngine.Timeline.TimelineAsset"/>, based on the <see cref="RefreshReason"/> provided.
  43. /// </para>
  44. /// <para>
  45. /// For better performance, it is recommended that you invoke this method once, after you modify the
  46. /// <see cref="UnityEngine.Timeline.TimelineAsset"/>. You should also combine reasons using the <c>|</c> operator.
  47. /// </para>
  48. /// </summary>
  49. /// <remarks>
  50. /// Note: This operation is not synchronous. It is performed during the next GUI loop.
  51. /// </remarks>
  52. /// <param name="reason">The reason why a refresh should be performed.</param>
  53. public static void Refresh(RefreshReason reason)
  54. {
  55. if (state == null)
  56. return;
  57. if ((reason & RefreshReason.ContentsAddedOrRemoved) != 0)
  58. {
  59. state.Refresh();
  60. }
  61. else if ((reason & RefreshReason.ContentsModified) != 0)
  62. {
  63. state.rebuildGraph = true;
  64. }
  65. else if ((reason & RefreshReason.SceneNeedsUpdate) != 0)
  66. {
  67. state.Evaluate();
  68. }
  69. window.Repaint();
  70. }
  71. static TimelineWindow window { get { return TimelineWindow.instance; } }
  72. static WindowState state { get { return window == null ? null : window.state; } }
  73. internal static readonly Clipboard clipboard = new Clipboard();
  74. /// <summary>
  75. /// The list of clips selected in the TimelineEditor.
  76. /// </summary>
  77. public static TimelineClip[] selectedClips
  78. {
  79. get { return Selection.GetFiltered<EditorClip>(SelectionMode.Unfiltered).Select(e => e.clip).Where(x => x != null).ToArray(); }
  80. set
  81. {
  82. if (value == null || value.Length == 0)
  83. {
  84. Selection.objects = null;
  85. }
  86. else
  87. {
  88. var objects = new List<UnityEngine.Object>();
  89. foreach (var clip in value)
  90. {
  91. if (clip == null)
  92. continue;
  93. var editorClip = EditorClipFactory.GetEditorClip(clip);
  94. if (editorClip != null)
  95. objects.Add(editorClip);
  96. }
  97. Selection.objects = objects.ToArray();
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// The clip selected in the TimelineEditor.
  103. /// </summary>
  104. /// <remarks>
  105. /// If there are multiple clips selected, this property returns the first clip.
  106. /// </remarks>
  107. public static TimelineClip selectedClip
  108. {
  109. get
  110. {
  111. var editorClip = Selection.activeObject as EditorClip;
  112. if (editorClip != null)
  113. return editorClip.clip;
  114. return null;
  115. }
  116. set
  117. {
  118. var editorClip = (value != null) ? EditorClipFactory.GetEditorClip(value) : null;
  119. Selection.activeObject = editorClip;
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// <see cref="TimelineEditor.Refresh"/> uses these flags to determine what needs to be refreshed or updated.
  125. /// </summary>
  126. /// <remarks>
  127. /// Use the <c>|</c> operator to combine flags.
  128. /// <example>
  129. /// <c>TimelineEditor.Refresh(RefreshReason.ContentsModified | RefreshReason.SceneNeedsUpdate);</c>
  130. /// </example>
  131. /// </remarks>
  132. [Flags]
  133. public enum RefreshReason
  134. {
  135. /// <summary>
  136. /// Use this flag when a change to the Timeline requires that the Timeline window be redrawn.
  137. /// </summary>
  138. WindowNeedsRedraw = 1 << 0,
  139. /// <summary>
  140. /// Use this flag when a change to the Timeline requires that the Scene be updated.
  141. /// </summary>
  142. SceneNeedsUpdate = 1 << 1,
  143. /// <summary>
  144. /// Use this flag when a Timeline element was modified.
  145. /// </summary>
  146. ContentsModified = 1 << 2,
  147. /// <summary>
  148. /// Use this flag when an element was added to or removed from the Timeline.
  149. /// </summary>
  150. ContentsAddedOrRemoved = 1 << 3
  151. }
  152. }