File size: 2,338 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Timeline;
using Object = UnityEngine.Object;
namespace UnityEditor.Timeline
{
/// <summary>
/// Disposable scope object used to collect multiple items for Undo.
/// Automatically filters out duplicates
/// </summary>
struct UndoScope : IDisposable
{
private static readonly HashSet<UnityEngine.Object> s_ObjectsToUndo = new HashSet<Object>();
private string m_Name;
public UndoScope(string name)
{
m_Name = name;
}
public void Dispose()
{
ApplyUndo(m_Name);
}
public void AddObject(UnityEngine.Object asset)
{
if (asset != null)
s_ObjectsToUndo.Add(asset);
}
public void AddClip(TimelineClip clip, bool includeAsset)
{
if (clip != null && clip.GetParentTrack() != null)
s_ObjectsToUndo.Add(clip.GetParentTrack());
if (includeAsset && clip != null && clip.asset != null)
s_ObjectsToUndo.Add(clip.asset);
}
public void Add(IEnumerable<TrackAsset> tracks)
{
if (tracks == null)
return;
foreach (var track in tracks)
AddObject(track);
}
public void Add(IEnumerable<TimelineClip> clips, bool includeAssets)
{
if (clips == null)
return;
foreach (var clip in clips)
{
AddClip(clip, includeAssets);
}
}
public void Add(IEnumerable<IMarker> markers)
{
if (markers == null)
return;
foreach (var marker in markers)
{
if (marker is Object o)
AddObject(o);
else if (marker != null)
AddObject(marker.parent);
}
}
private static void ApplyUndo(string name)
{
if (s_ObjectsToUndo.Count == 1)
TimelineUndo.PushUndo(s_ObjectsToUndo.First(), name);
else if (s_ObjectsToUndo.Count > 1)
TimelineUndo.PushUndo(s_ObjectsToUndo.ToArray(), name);
s_ObjectsToUndo.Clear();
}
}
}
|