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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections.Generic;
  2. using UnityEditor.Timeline.Actions;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. using UnityEngine.Timeline;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Timeline
  8. {
  9. /// <summary>
  10. /// Use this class to record the state of a timeline or its components prior to modification.
  11. /// </summary>
  12. /// <remarks>
  13. /// These methods do not need to be used when adding or deleting tracks, clips or markers.
  14. /// Methods in the UnityEngine.Timeline namespace, such as <see cref="UnityEngine.Timeline.TimelineAsset.CreateTrack"/>
  15. /// or <see cref="UnityEngine.Timeline.TrackAsset.CreateDefaultClip"/> will apply the appropriate
  16. /// Undo calls when called in Editor.
  17. /// </remarks>
  18. public static class UndoExtensions
  19. {
  20. /// <summary>
  21. /// Records all items contained in an action context. Use this method to record all objects
  22. /// inside the 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 to appear in the undo history (i.e. visible 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 any changes done on the timeline after being called. This only applies
  37. /// to the timeline asset properties itself, and not any of the tracks or clips on the timeline
  38. /// </summary>
  39. /// <param name="asset">The timeline asset being modified.</param>
  40. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible 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 any changes done on the timeline after being called, including any changes
  48. /// to any clips, tracks and markers that occur on the timeline.
  49. /// </summary>
  50. /// <param name="asset">The timeline asset being modified.</param>
  51. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  52. public static void RegisterCompleteTimeline(TimelineAsset asset, string undoTitle)
  53. {
  54. if (asset == null)
  55. return;
  56. using (var undo = new UndoScope(undoTitle))
  57. {
  58. undo.AddObject(asset);
  59. undo.Add(asset.flattenedTracks);
  60. foreach (var t in asset.flattenedTracks)
  61. {
  62. undo.Add(t.GetClips(), true);
  63. undo.Add(t.GetMarkers());
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// Records any changes done on the track after being called, including any changes
  69. /// to clips on the track, but not on markers or PlayableAssets attached to the clips.
  70. /// </summary>
  71. /// <param name="asset">The timeline track being modified.</param>
  72. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  73. public static void RegisterTrack(TrackAsset asset, string undoTitle)
  74. {
  75. using (var undo = new UndoScope(undoTitle))
  76. undo.AddObject(asset);
  77. }
  78. /// <summary>
  79. /// Records any changes done on the tracks after being called, including any changes
  80. /// to clips on the tracks, but not on markers or PlayableAssets attached to the clips.
  81. /// </summary>
  82. /// <param name="tracks">The timeline track being modified.</param>
  83. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  84. public static void RegisterTracks(IEnumerable<TrackAsset> tracks, string undoTitle)
  85. {
  86. using (var undo = new UndoScope(undoTitle))
  87. undo.Add(tracks);
  88. }
  89. /// <summary>
  90. /// Records any changes done on the clip after being called.
  91. /// </summary>
  92. /// <param name="clip">The timeline clip being modified.</param>
  93. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  94. /// <param name="includePlayableAsset">Set this value to true to also record changes on the attached playable asset.</param>
  95. public static void RegisterClip(TimelineClip clip, string undoTitle, bool includePlayableAsset = true)
  96. {
  97. using (var undo = new UndoScope(undoTitle))
  98. {
  99. undo.AddClip(clip, includePlayableAsset);
  100. }
  101. }
  102. /// <summary>
  103. /// Records any changes done on the PlayableAsset after being called.
  104. /// </summary>
  105. /// <param name="asset">The timeline track being modified.</param>
  106. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  107. public static void RegisterPlayableAsset(PlayableAsset asset, string undoTitle)
  108. {
  109. using (var undo = new UndoScope(undoTitle))
  110. undo.AddObject(asset);
  111. }
  112. /// <summary>
  113. /// Records any changes done on the clips after being called.
  114. /// </summary>
  115. /// <param name="clips">The timeline clips being modified.</param>
  116. /// <param name="name">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  117. /// <param name="includePlayableAssets">Set this value to true to also record changes on the attached playable assets.</param>
  118. public static void RegisterClips(IEnumerable<TimelineClip> clips, string name, bool includePlayableAssets = true)
  119. {
  120. using (var undo = new UndoScope(name))
  121. undo.Add(clips, includePlayableAssets);
  122. }
  123. /// <summary>
  124. /// Records any changes done on the Timeline Marker after being called.
  125. /// </summary>
  126. /// <param name="marker">The timeline clip being modified.</param>
  127. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  128. public static void RegisterMarker(IMarker marker, string undoTitle)
  129. {
  130. using (var undo = new UndoScope(undoTitle))
  131. {
  132. if (marker is Object o)
  133. undo.AddObject(o);
  134. else if (marker != null)
  135. undo.AddObject(marker.parent);
  136. }
  137. }
  138. /// <summary>
  139. /// Records any changes done on the Timeline Markers after being called.
  140. /// </summary>
  141. /// <param name="markers">The timeline clip being modified.</param>
  142. /// <param name="undoTitle">The title of the action to appear in the undo history (i.e. visible in the undo menu).</param>
  143. public static void RegisterMarkers(IEnumerable<IMarker> markers, string undoTitle)
  144. {
  145. using (var undo = new UndoScope(undoTitle))
  146. undo.Add(markers);
  147. }
  148. }
  149. }