No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimelineEditor.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Timeline.Actions;
  5. using UnityEngine;
  6. using UnityEngine.Playables;
  7. using UnityEngine.Timeline;
  8. namespace UnityEditor.Timeline
  9. {
  10. /// <summary>
  11. /// Information currently being edited in the Timeline Editor Window.
  12. /// </summary>
  13. public static class TimelineEditor
  14. {
  15. /// <summary>
  16. /// Returns a reference to the Timeline Window.
  17. /// </summary>
  18. /// <returns>A reference to the TimelineWindow and null if the window is not opened.</returns>
  19. public static TimelineEditorWindow GetWindow()
  20. {
  21. return window;
  22. }
  23. /// <summary>
  24. /// Returns a reference to the Timeline Window. If the window is not opened, it will be opened.
  25. /// </summary>
  26. /// <returns>A reference to the TimelineWindow.</returns>
  27. public static TimelineEditorWindow GetOrCreateWindow()
  28. {
  29. if (window != null)
  30. return window;
  31. return EditorWindow.GetWindow<TimelineWindow>(false, null, false);
  32. }
  33. /// <summary>
  34. /// The PlayableDirector associated with the timeline currently being shown in the Timeline window.
  35. /// </summary>
  36. public static PlayableDirector inspectedDirector => state?.editSequence.director;
  37. /// <summary>
  38. /// The PlayableDirector responsible for the playback of the timeline currently being shown in the Timeline window.
  39. /// </summary>
  40. public static PlayableDirector masterDirector => state?.masterSequence.director;
  41. /// <summary>
  42. /// The TimelineAsset currently being shown in the Timeline window.
  43. /// </summary>
  44. public static TimelineAsset inspectedAsset => state?.editSequence.asset;
  45. /// <summary>
  46. /// The TimelineAsset at the root of the hierarchy currently being shown in the Timeline window.
  47. /// </summary>
  48. public static TimelineAsset masterAsset => state?.masterSequence.asset;
  49. /// <summary>
  50. /// The PlayableDirector currently being shown in the Timeline Editor Window.
  51. /// </summary>
  52. [Obsolete("playableDirector is ambiguous. Please select either inspectedDirector or masterDirector instead.", false)]
  53. public static PlayableDirector playableDirector
  54. {
  55. get { return inspectedDirector; }
  56. }
  57. /// <summary>
  58. /// The TimelineAsset currently being shown in the Timeline Editor Window.
  59. /// </summary>
  60. [Obsolete("timelineAsset is ambiguous. Please select either inspectedAsset or masterAsset instead.", false)]
  61. public static TimelineAsset timelineAsset
  62. {
  63. get { return inspectedAsset; }
  64. }
  65. /// <summary>
  66. /// <para>
  67. /// Refreshes the different components affected by the currently inspected
  68. /// <see cref="UnityEngine.Timeline.TimelineAsset"/>, based on the <see cref="RefreshReason"/> provided.
  69. /// </para>
  70. /// <para>
  71. /// For better performance, it is recommended that you invoke this method once, after you modify the
  72. /// <see cref="UnityEngine.Timeline.TimelineAsset"/>. You should also combine reasons using the <c>|</c> operator.
  73. /// </para>
  74. /// </summary>
  75. /// <remarks>
  76. /// Note: This operation is not synchronous. It is performed during the next GUI loop.
  77. /// </remarks>
  78. /// <param name="reason">The reason why a refresh should be performed.</param>
  79. public static void Refresh(RefreshReason reason)
  80. {
  81. if (state == null)
  82. return;
  83. if ((reason & RefreshReason.ContentsAddedOrRemoved) != 0)
  84. {
  85. state.Refresh();
  86. }
  87. else if ((reason & RefreshReason.ContentsModified) != 0)
  88. {
  89. state.rebuildGraph = true;
  90. }
  91. else if ((reason & RefreshReason.SceneNeedsUpdate) != 0)
  92. {
  93. state.Evaluate();
  94. }
  95. window.Repaint();
  96. }
  97. internal static TimelineWindow window => TimelineWindow.instance;
  98. internal static WindowState state => window == null ? null : window.state;
  99. internal static readonly Clipboard clipboard = new Clipboard();
  100. /// <summary>
  101. /// The list of clips selected in the TimelineEditor.
  102. /// </summary>
  103. public static TimelineClip[] selectedClips
  104. {
  105. get { return Selection.GetFiltered<EditorClip>(SelectionMode.Unfiltered).Select(e => e.clip).Where(x => x != null).ToArray(); }
  106. set
  107. {
  108. if (value == null || value.Length == 0)
  109. {
  110. Selection.objects = null;
  111. }
  112. else
  113. {
  114. var objects = new List<UnityEngine.Object>();
  115. foreach (var clip in value)
  116. {
  117. if (clip == null)
  118. continue;
  119. var editorClip = EditorClipFactory.GetEditorClip(clip);
  120. if (editorClip != null)
  121. objects.Add(editorClip);
  122. }
  123. Selection.objects = objects.ToArray();
  124. }
  125. }
  126. }
  127. /// <summary>
  128. /// The clip selected in the TimelineEditor.
  129. /// </summary>
  130. /// <remarks>
  131. /// If there are multiple clips selected, this property returns the first clip.
  132. /// </remarks>
  133. public static TimelineClip selectedClip
  134. {
  135. get
  136. {
  137. var editorClip = Selection.activeObject as EditorClip;
  138. if (editorClip != null)
  139. return editorClip.clip;
  140. return null;
  141. }
  142. set
  143. {
  144. var editorClip = (value != null) ? EditorClipFactory.GetEditorClip(value) : null;
  145. Selection.activeObject = editorClip;
  146. }
  147. }
  148. /// <summary>
  149. /// Local time (in seconds) of the inspected sequence.
  150. /// </summary>
  151. /// <exception cref="InvalidOperationException">Thrown if timeline window is not available.</exception>
  152. internal static double inspectedSequenceTime
  153. {
  154. get => state?.editSequence.time ?? 0;
  155. set
  156. {
  157. if (state == null)
  158. throw new InvalidOperationException("Cannot set time. Timeline Window may not be available.");
  159. state.editSequence.time = value;
  160. }
  161. }
  162. /// <summary>
  163. /// Global time (in seconds) of the master timeline.
  164. /// Same as local time if not inspected a subtimeline.
  165. /// </summary>
  166. /// <exception cref="InvalidOperationException">Thrown if timeline window is not available.</exception>
  167. internal static double masterSequenceTime
  168. {
  169. get => state?.editSequence.ToGlobalTime(state.editSequence.time) ?? 0;
  170. set
  171. {
  172. if (state == null)
  173. throw new InvalidOperationException("Cannot set time. Timeline Window may not be available.");
  174. state.masterSequence.time = value;
  175. }
  176. }
  177. /// <summary>
  178. /// Visible time range (in seconds) in Editor.
  179. /// x : min time
  180. /// y : max time
  181. /// </summary>
  182. /// <exception cref="InvalidOperationException">Thrown if timeline window is not available.</exception>
  183. internal static Vector2 visibleTimeRange
  184. {
  185. get => state?.timeAreaShownRange ?? TimelineAssetViewModel.TimeAreaDefaultRange;
  186. set
  187. {
  188. if (state == null)
  189. throw new InvalidOperationException("Cannot set visible time range. Timeline Window may not be available.");
  190. state.timeAreaShownRange = value;
  191. }
  192. }
  193. internal static ActionContext CurrentContext(Vector2? mousePos = null)
  194. {
  195. return new ActionContext
  196. {
  197. invocationTime = mousePos != null ? TimelineHelpers.GetCandidateTime(mousePos) : (double?)null,
  198. clips = SelectionManager.SelectedClips(),
  199. tracks = SelectionManager.SelectedTracks(),
  200. markers = SelectionManager.SelectedMarkers(),
  201. timeline = inspectedAsset,
  202. director = inspectedDirector
  203. };
  204. }
  205. /// <summary>
  206. /// Converts time from the master timeline to the current inspected timeline.
  207. /// </summary>
  208. /// <param name="masterTime">Time in the referential of the main timeline</param>
  209. /// <returns>Time in the referential of the sub-timeline that is currently show.
  210. /// Returns <paramref name="masterTime"/> if there is no sub-timeline or if no timeline is shown.</returns>
  211. public static double GetInspectedTimeFromMasterTime(double masterTime)
  212. {
  213. ISequenceState editSequence = state?.editSequence;
  214. if (editSequence == null)
  215. return masterTime;
  216. return state.editSequence.ToLocalTime(masterTime);
  217. }
  218. /// <summary>
  219. /// Converts time from the current inspected timeline to the master timeline.
  220. /// </summary>
  221. /// <param name="inspectedTime">Time in the referential of the sub-timeline</param>
  222. /// <returns>Time in the referential of the main timeline.
  223. /// Returns <paramref name="inspectedTime"/> if there if no timeline is shown.</returns>
  224. public static double GetMasterTimeFromInspectedTime(double inspectedTime)
  225. {
  226. ISequenceState editSequence = state?.editSequence;
  227. if (editSequence == null)
  228. return inspectedTime;
  229. return editSequence.ToGlobalTime(inspectedTime);
  230. }
  231. internal static void RefreshPreviewPlay()
  232. {
  233. if (state == null || !state.playing)
  234. return;
  235. state.Pause();
  236. state.Play();
  237. }
  238. }
  239. /// <summary>
  240. /// <see cref="TimelineEditor.Refresh"/> uses these flags to determine what needs to be refreshed or updated.
  241. /// </summary>
  242. /// <remarks>
  243. /// Use the <c>|</c> operator to combine flags.
  244. /// <example>
  245. /// <code source="../DocCodeExamples/TimelineEditorExamples.cs" region="declare-refreshReason" title="refreshReason"/>
  246. /// </example>
  247. /// </remarks>
  248. [Flags]
  249. public enum RefreshReason
  250. {
  251. /// <summary>
  252. /// Use this flag when a change to the Timeline requires that the Timeline window be redrawn.
  253. /// </summary>
  254. WindowNeedsRedraw = 1 << 0,
  255. /// <summary>
  256. /// Use this flag when a change to the Timeline requires that the Scene be updated.
  257. /// </summary>
  258. SceneNeedsUpdate = 1 << 1,
  259. /// <summary>
  260. /// Use this flag when a Timeline element was modified.
  261. /// </summary>
  262. ContentsModified = 1 << 2,
  263. /// <summary>
  264. /// Use this flag when an element was added to or removed from the Timeline.
  265. /// </summary>
  266. ContentsAddedOrRemoved = 1 << 3
  267. }
  268. }