using System.Collections.Generic; using UnityEditor.Timeline.Actions; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; using Object = UnityEngine.Object; namespace UnityEditor.Timeline { /// /// Use this class to record the state of a timeline or its components prior to modification. /// /// /// These methods do not need to be used when adding or deleting tracks, clips or markers. /// Methods in the UnityEngine.Timeline namespace, such as /// or will apply the appropriate /// Undo calls when called in Editor. /// public static class UndoExtensions { /// /// Records all items contained in an action context. Use this method to record all objects /// inside the context. /// /// The action context to record into the Undo system. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterContext(ActionContext context, string undoTitle) { using (var undo = new UndoScope(undoTitle)) { undo.Add(context.tracks); undo.Add(context.clips, true); undo.Add(context.markers); } } /// /// Records any changes done on the timeline after being called. This only applies /// to the timeline asset properties itself, and not any of the tracks or clips on the timeline /// /// The timeline asset being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterTimeline(TimelineAsset asset, string undoTitle) { using (var undo = new UndoScope(undoTitle)) undo.AddObject(asset); } /// /// Records any changes done on the timeline after being called, including any changes /// to any clips, tracks and markers that occur on the timeline. /// /// The timeline asset being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterCompleteTimeline(TimelineAsset asset, string undoTitle) { if (asset == null) return; using (var undo = new UndoScope(undoTitle)) { undo.AddObject(asset); undo.Add(asset.flattenedTracks); foreach (var t in asset.flattenedTracks) { undo.Add(t.GetClips(), true); undo.Add(t.GetMarkers()); } } } /// /// Records any changes done on the track after being called, including any changes /// to clips on the track, but not on markers or PlayableAssets attached to the clips. /// /// The timeline track being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterTrack(TrackAsset asset, string undoTitle) { using (var undo = new UndoScope(undoTitle)) undo.AddObject(asset); } /// /// Records any changes done on the tracks after being called, including any changes /// to clips on the tracks, but not on markers or PlayableAssets attached to the clips. /// /// The timeline track being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterTracks(IEnumerable tracks, string undoTitle) { using (var undo = new UndoScope(undoTitle)) undo.Add(tracks); } /// /// Records any changes done on the clip after being called. /// /// The timeline clip being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). /// Set this value to true to also record changes on the attached playable asset. public static void RegisterClip(TimelineClip clip, string undoTitle, bool includePlayableAsset = true) { using (var undo = new UndoScope(undoTitle)) { undo.AddClip(clip, includePlayableAsset); } } /// /// Records any changes done on the PlayableAsset after being called. /// /// The timeline track being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterPlayableAsset(PlayableAsset asset, string undoTitle) { using (var undo = new UndoScope(undoTitle)) undo.AddObject(asset); } /// /// Records any changes done on the clips after being called. /// /// The timeline clips being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). /// Set this value to true to also record changes on the attached playable assets. public static void RegisterClips(IEnumerable clips, string name, bool includePlayableAssets = true) { using (var undo = new UndoScope(name)) undo.Add(clips, includePlayableAssets); } /// /// Records any changes done on the Timeline Marker after being called. /// /// The timeline clip being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterMarker(IMarker marker, string undoTitle) { using (var undo = new UndoScope(undoTitle)) { if (marker is Object o) undo.AddObject(o); else if (marker != null) undo.AddObject(marker.parent); } } /// /// Records any changes done on the Timeline Markers after being called. /// /// The timeline clip being modified. /// The title of the action to appear in the undo history (i.e. visible in the undo menu). public static void RegisterMarkers(IEnumerable markers, string undoTitle) { using (var undo = new UndoScope(undoTitle)) undo.Add(markers); } } }