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.

UndoExtensions.cs 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Timeline.Actions;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. using UnityEngine.Timeline;
  7. using Object = UnityEngine.Object;
  8. namespace UnityEditor.Timeline
  9. {
  10. /// <summary>
  11. /// Provides methods that record the state of a timeline, and its components, prior to modification.
  12. /// </summary>
  13. /// <remarks>
  14. /// The methods in this class are not required when adding or deleting tracks, clips, or markers.
  15. /// Use methods in the UnityEngine.Timeline namespace, such as <see cref="UnityEngine.Timeline.TimelineAsset.CreateTrack"/>
  16. /// or <see cref="UnityEngine.Timeline.TrackAsset.CreateDefaultClip"/>, to apply the appropriate
  17. /// Undo calls when using the Editor.
  18. /// </remarks>
  19. public static class UndoExtensions
  20. {
  21. /// <summary>
  22. /// Records changes to all items contained in an action context.
  23. /// </summary>
  24. /// <param name="context">The action context to record into the Undo system.</param>
  25. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  26. public static void RegisterContext(ActionContext context, string undoTitle)
  27. {
  28. using (var undo = new UndoScope(undoTitle))
  29. {
  30. undo.Add(context.tracks);
  31. undo.Add(context.clips, true);
  32. undo.Add(context.markers);
  33. }
  34. }
  35. /// <summary>
  36. /// Records changes to timeline asset properties.
  37. /// This method does not record changes to tracks or clips.
  38. /// </summary>
  39. /// <param name="asset">The timeline asset being modified.</param>
  40. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  41. public static void RegisterTimeline(TimelineAsset asset, string undoTitle)
  42. {
  43. using (var undo = new UndoScope(undoTitle))
  44. undo.AddObject(asset);
  45. }
  46. /// <summary>
  47. /// Records all timeline changes including changes to tracks, clips, and markers.
  48. /// </summary>
  49. /// <param name="asset">The timeline asset being modified.</param>
  50. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  51. public static void RegisterCompleteTimeline(TimelineAsset asset, string undoTitle)
  52. {
  53. if (asset == null)
  54. return;
  55. using (var undo = new UndoScope(undoTitle))
  56. {
  57. undo.AddObject(asset);
  58. undo.Add(asset.flattenedTracks);
  59. foreach (var t in asset.flattenedTracks)
  60. {
  61. undo.Add(t.GetClips(), true);
  62. undo.Add(t.GetMarkers());
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// Records changes to tracks and clips but not to markers nor PlayableAssets attached to clips.
  68. /// </summary>
  69. /// <param name="asset">The timeline track being modified.</param>
  70. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  71. public static void RegisterTrack(TrackAsset asset, string undoTitle)
  72. {
  73. using (var undo = new UndoScope(undoTitle))
  74. undo.AddObject(asset);
  75. }
  76. /// <summary>
  77. /// Records changes to tracks. This includes changes
  78. /// to clips on these tracks, but not changes to markers nor PlayableAssets attached to clips.
  79. /// </summary>
  80. /// <param name="tracks">The timeline track being modified.</param>
  81. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  82. public static void RegisterTracks(IEnumerable<TrackAsset> tracks, string undoTitle)
  83. {
  84. using (var undo = new UndoScope(undoTitle))
  85. undo.Add(tracks);
  86. }
  87. /// <summary>
  88. /// Records changes to a clip.
  89. /// </summary>
  90. /// <param name="clip">The timeline clip being modified.</param>
  91. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  92. /// <param name="includePlayableAsset">Set this value to true to record changes to the attached playable asset.</param>
  93. public static void RegisterClip(TimelineClip clip, string undoTitle, bool includePlayableAsset = true)
  94. {
  95. using (var undo = new UndoScope(undoTitle))
  96. {
  97. undo.AddClip(clip, includePlayableAsset);
  98. }
  99. }
  100. /// <summary>
  101. /// Records changes to a PlayableAsset.
  102. /// </summary>
  103. /// <param name="asset">The PlayableAsset being modified.</param>
  104. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  105. public static void RegisterPlayableAsset(PlayableAsset asset, string undoTitle)
  106. {
  107. using (var undo = new UndoScope(undoTitle))
  108. undo.AddObject(asset);
  109. }
  110. /// <summary>
  111. /// Records changes to clips.
  112. /// </summary>
  113. /// <param name="clips">The timeline clips being modified.</param>
  114. /// <param name="name">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  115. /// <param name="includePlayableAssets">Set this value to true to also record changes to attached playable assets.</param>
  116. public static void RegisterClips(IEnumerable<TimelineClip> clips, string name, bool includePlayableAssets = true)
  117. {
  118. using (var undo = new UndoScope(name))
  119. undo.Add(clips, includePlayableAssets);
  120. }
  121. /// <summary>
  122. /// Records changes to a timeline marker.
  123. /// </summary>
  124. /// <param name="marker">The timeline marker being modified.</param>
  125. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  126. public static void RegisterMarker(IMarker marker, string undoTitle)
  127. {
  128. using (var undo = new UndoScope(undoTitle))
  129. {
  130. if (marker is Object o)
  131. undo.AddObject(o);
  132. else if (marker != null)
  133. undo.AddObject(marker.parent);
  134. }
  135. }
  136. /// <summary>
  137. /// Records changes to timeline markers.
  138. /// </summary>
  139. /// <param name="markers">The timeline markers being modified.</param>
  140. /// <param name="undoTitle">The title of the action that appears in the undo history. For example, this title is shown in the Undo menu.</param>
  141. public static void RegisterMarkers(IEnumerable<IMarker> markers, string undoTitle)
  142. {
  143. using (var undo = new UndoScope(undoTitle))
  144. undo.Add(markers);
  145. }
  146. /// <summary>
  147. /// This class provides an object which prevents the creation of undos for all Timeline operations. Undos are restored when the object is disposed.
  148. /// </summary>
  149. /// <remarks>
  150. /// Use this class to procedurally create or modify TimelineAssets when undos are not needed.
  151. /// If multiple DisableTimelineUndoScope instances are created, undos are only restored after all instances are disposed.
  152. ///
  153. /// It is recommended to use this object within a using scope.
  154. /// </remarks>
  155. /// <example>
  156. /// <code>
  157. /// var timelineAsset = new TimelineAsset();
  158. /// using (new DisableTimelineUndoScope())
  159. /// {
  160. /// //Creates a track without generating an undo
  161. /// timelineAsset.CreateTrack<ActivationTrack>();
  162. /// }
  163. /// </code>
  164. /// </example>
  165. internal sealed class DisableTimelineUndoScope : IDisposable
  166. {
  167. TimelineUndo.DisableUndoScope m_DisableScope;
  168. /// <summary>
  169. /// Creates a new DisableTimelineUndoScope object which prevents undos from being created by Timeline operations.
  170. /// </summary>
  171. public DisableTimelineUndoScope()
  172. {
  173. m_DisableScope = new TimelineUndo.DisableUndoScope();
  174. }
  175. /// <summary>
  176. /// Disposes the DisableTimelineUndoScope object.
  177. /// </summary>
  178. public void Dispose()
  179. {
  180. m_DisableScope.Dispose();
  181. }
  182. }
  183. }
  184. }