SequenceHierarchy.cs
9.08 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
class SequenceHierarchy : ScriptableObject
{
readonly List<ISequenceState> m_Sequences = new List<ISequenceState>();
WindowState m_WindowState;
[SerializeField]
SequencePath m_SerializedPath;
public ISequenceState masterSequence
{
get { return m_Sequences.FirstOrDefault(); }
}
public ISequenceState editSequence
{
get { return m_Sequences.LastOrDefault(); }
}
public int count
{
get { return m_Sequences.Count; }
}
public IEnumerable<ISequenceState> allSequences
{
get { return m_Sequences; }
}
public static SequenceHierarchy CreateInstance()
{
var hierarchy = ScriptableObject.CreateInstance<SequenceHierarchy>();
hierarchy.hideFlags = HideFlags.HideAndDontSave;
return hierarchy;
}
public void Init(WindowState owner)
{
m_WindowState = owner;
}
// This is called when performing Undo operations.
// It needs to be called here since some operations are not
// allowed (EditorUtility.InstanceIDToObject, for example)
// during the ISerializationCallbackReceiver methods.
void OnValidate()
{
if (m_SerializedPath == null || m_WindowState == null || m_WindowState.GetWindow() == null)
return;
m_WindowState.SetCurrentSequencePath(m_SerializedPath, true);
}
public void Add(TimelineAsset asset, PlayableDirector director, TimelineClip hostClip)
{
if (hostClip == null)
AddToCurrentUndoGroup(this); // Merge with selection undo
else
TimelineUndo.PushUndo(this, "Edit Sub-Timeline");
Add_Internal(asset, director, hostClip);
UpdateSerializedPath();
}
public void Remove()
{
if (m_Sequences.Count == 0) return;
TimelineUndo.PushUndo(this, "Go to Sub-Timeline");
Remove_Internal();
UpdateSerializedPath();
}
public ISequenceState GetStateAtIndex(int index)
{
return m_Sequences[index];
}
public void RemoveUntilCount(int expectedCount)
{
if (expectedCount < 0 || m_Sequences.Count <= expectedCount) return;
TimelineUndo.PushUndo(this, "Go to Sub-Timeline");
RemoveUntilCount_Internal(expectedCount);
UpdateSerializedPath();
}
public void Clear()
{
if (m_Sequences.Count == 0) return;
AddToCurrentUndoGroup(this);
Clear_Internal();
UpdateSerializedPath();
}
public SequencePath ToSequencePath()
{
var path = new SequencePath();
if (m_Sequences.Count == 0)
return path;
var rootSequence = m_Sequences[0];
var root = 0;
if (rootSequence.director != null && rootSequence.director.gameObject != null)
root = rootSequence.director.gameObject.GetInstanceID();
else if (rootSequence.asset != null)
root = rootSequence.asset.GetInstanceID();
path.SetSelectionRoot(root);
var resolver = rootSequence.director;
if (m_Sequences.Count > 1)
{
for (int i = 1, n = m_Sequences.Count; i < n; ++i)
{
path.AddSubSequence(m_Sequences[i], resolver);
resolver = m_Sequences[i].director;
}
}
return path;
}
public bool NeedsUpdate(SequencePath path, bool forceRebuild)
{
return forceRebuild || !SequencePath.AreEqual(m_SerializedPath, path);
}
public void FromSequencePath(SequencePath path, bool forceRebuild)
{
if (!NeedsUpdate(path, forceRebuild))
return;
Clear_Internal();
var rootObject = EditorUtility.InstanceIDToObject(path.selectionRoot);
if (rootObject == null)
{
UpdateSerializedPath();
return;
}
var candidateAsset = rootObject as TimelineAsset;
if (candidateAsset != null)
{
Add_Internal(candidateAsset, null, null);
UpdateSerializedPath();
return;
}
var candidateGameObject = rootObject as GameObject;
if (candidateGameObject == null)
{
UpdateSerializedPath();
return;
}
var director = TimelineUtility.GetDirectorComponentForGameObject(candidateGameObject);
var asset = TimelineUtility.GetTimelineAssetForDirectorComponent(director);
Add_Internal(asset, director, null);
if (!path.subElements.Any())
{
UpdateSerializedPath();
return;
}
List<SequenceBuildingBlock> buildingBlocks;
if (ValidateSubElements(path.subElements, director, out buildingBlocks))
{
foreach (var buildingBlock in buildingBlocks)
Add_Internal(buildingBlock.asset, buildingBlock.director, buildingBlock.hostClip);
}
UpdateSerializedPath();
}
void Add_Internal(TimelineAsset asset, PlayableDirector director, TimelineClip hostClip)
{
if (hostClip == null)
Clear_Internal();
var parent = m_Sequences.Count > 0 ? editSequence : null;
m_Sequences.Add(new SequenceState(m_WindowState, asset, director, hostClip, (SequenceState)parent));
}
void Remove_Internal()
{
m_Sequences.Last().Dispose();
m_Sequences.RemoveAt(m_Sequences.Count - 1);
}
void RemoveUntilCount_Internal(int expectedCount)
{
while (m_Sequences.Count > expectedCount)
{
Remove_Internal();
}
}
void Clear_Internal()
{
RemoveUntilCount_Internal(0);
}
void UpdateSerializedPath()
{
m_SerializedPath = ToSequencePath();
}
static bool ValidateSubElements(List<SequencePathSubElement> subElements, PlayableDirector director, out List<SequenceBuildingBlock> buildingBlocks)
{
buildingBlocks = new List<SequenceBuildingBlock>(subElements.Count);
var currentDirector = director;
foreach (var element in subElements)
{
var timeline = currentDirector.playableAsset as TimelineAsset;
if (timeline == null)
return false;
if (timeline.trackObjects == null)
return false;
var track = timeline.GetOutputTracks().FirstOrDefault(t => t.GetInstanceID() == element.trackInstanceID);
if (track == null)
return false;
if (track.Hash() != element.trackHash)
return false;
if (track.clips == null)
return false;
if (track.clips.Length <= element.clipIndex)
return false;
var clip = track.clips[element.clipIndex];
if (clip == null)
return false;
if (clip.Hash() != element.clipHash)
return false;
var candidateDirectors = TimelineUtility.GetSubTimelines(clip, director);
if (element.subDirectorIndex < 0 || element.subDirectorIndex >= candidateDirectors.Count)
return false;
var candidateDirector = candidateDirectors[element.subDirectorIndex];
if (candidateDirector == null || !(candidateDirector.playableAsset is TimelineAsset))
return false;
currentDirector = candidateDirector;
buildingBlocks.Add(
new SequenceBuildingBlock
{
asset = currentDirector.playableAsset as TimelineAsset,
director = currentDirector,
hostClip = clip
});
}
return true;
}
struct SequenceBuildingBlock
{
public TimelineAsset asset;
public PlayableDirector director;
public TimelineClip hostClip;
}
static void AddToCurrentUndoGroup(Object target)
{
if (target == null) return;
var group = Undo.GetCurrentGroup();
var groupName = Undo.GetCurrentGroupName();
EditorUtility.SetDirty(target);
Undo.RegisterCompleteObjectUndo(target, groupName);
Undo.CollapseUndoOperations(group);
}
}
}