TimelineUtility.cs
11.6 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using Object = UnityEngine.Object;
using UnityEditor.Experimental.SceneManagement;
namespace UnityEditor.Timeline
{
static class TimelineUtility
{
public static void ReorderTracks(List<ScriptableObject> allTracks, List<TrackAsset> tracks, ScriptableObject insertAfterAsset, bool up)
{
foreach (var i in tracks)
allTracks.Remove(i);
int index = allTracks.IndexOf(insertAfterAsset);
index = up ? Math.Max(index, 0) : index + 1;
allTracks.InsertRange(index, tracks.OfType<ScriptableObject>());
}
// Gets the track that holds the game object reference for this track.
public static TrackAsset GetSceneReferenceTrack(TrackAsset asset)
{
if (asset == null)
return null;
if (asset.isSubTrack)
return GetSceneReferenceTrack(asset.parent as TrackAsset);
return asset;
}
public static bool TrackHasAnimationCurves(TrackAsset track)
{
if (track.hasCurves)
return true;
var animTrack = track as AnimationTrack;
if (animTrack != null && animTrack.infiniteClip != null)
return true;
for (int i = 0; i < track.clips.Length; i++)
{
var curveClip = track.clips[i].curves;
var animationClip = track.clips[i].animationClip;
// prune out clip with zero curves
if (curveClip != null && curveClip.empty)
curveClip = null;
if (animationClip != null && animationClip.empty)
animationClip = null;
// prune out clips coming from FBX
if (animationClip != null && ((animationClip.hideFlags & HideFlags.NotEditable) != 0))
animationClip = null;
if (!track.clips[i].recordable)
animationClip = null;
if ((curveClip != null) || (animationClip != null))
return true;
}
return false;
}
// get the game object reference associated with this
public static GameObject GetSceneGameObject(PlayableDirector director, TrackAsset asset)
{
if (director == null || asset == null)
return null;
asset = GetSceneReferenceTrack(asset);
var gameObject = director.GetGenericBinding(asset) as GameObject;
var component = director.GetGenericBinding(asset) as Component;
if (component != null)
gameObject = component.gameObject;
return gameObject;
}
public static void SetSceneGameObject(PlayableDirector director, TrackAsset asset, GameObject go)
{
if (director == null || asset == null)
return;
asset = GetSceneReferenceTrack(asset);
var bindings = asset.outputs;
if (bindings.Count() == 0)
return;
var binding = bindings.First();
if (binding.outputTargetType == typeof(GameObject))
{
BindingUtility.Bind(director, asset, go);
}
else
{
BindingUtility.Bind(director, asset, TimelineHelpers.AddRequiredComponent(go, asset));
}
}
public static PlayableDirector[] GetDirectorsInSceneUsingAsset(PlayableAsset asset)
{
const HideFlags hideFlags =
HideFlags.HideInHierarchy | HideFlags.HideInInspector |
HideFlags.DontSaveInEditor | HideFlags.NotEditable;
var prefabMode = PrefabStageUtility.GetCurrentPrefabStage();
var inScene = new List<PlayableDirector>();
var allDirectors = Resources.FindObjectsOfTypeAll(typeof(PlayableDirector)) as PlayableDirector[];
foreach (var director in allDirectors)
{
if ((director.hideFlags & hideFlags) != 0)
continue;
string assetPath = AssetDatabase.GetAssetPath(director.transform.root.gameObject);
if (!String.IsNullOrEmpty(assetPath))
continue;
if (prefabMode != null && !prefabMode.IsPartOfPrefabContents(director.gameObject))
continue;
if (asset == null || (asset != null && director.playableAsset == asset))
{
inScene.Add(director);
}
}
return inScene.ToArray();
}
public static PlayableDirector GetDirectorComponentForGameObject(GameObject gameObject)
{
return gameObject != null ? gameObject.GetComponent<PlayableDirector>() : null;
}
public static TimelineAsset GetTimelineAssetForDirectorComponent(PlayableDirector director)
{
return director != null ? director.playableAsset as TimelineAsset : null;
}
public static bool IsPrefabOrAsset(Object obj)
{
return EditorUtility.IsPersistent(obj) || (obj.hideFlags & HideFlags.NotEditable) != 0;
}
// TODO -- Need to add this to SerializedProperty so we can get replicate the accuracy that exists
// in the undo system
internal static string PropertyToString(SerializedProperty property)
{
switch (property.propertyType)
{
case SerializedPropertyType.Integer:
return property.intValue.ToString();
case SerializedPropertyType.Float:
return property.floatValue.ToString();
case SerializedPropertyType.String:
return property.stringValue;
case SerializedPropertyType.Boolean:
return property.boolValue ? "1" : "0";
case SerializedPropertyType.Color:
return property.colorValue.ToString();
case SerializedPropertyType.ArraySize:
return property.intValue.ToString();
case SerializedPropertyType.Enum:
return property.intValue.ToString();
case SerializedPropertyType.ObjectReference:
return string.Empty;
case SerializedPropertyType.LayerMask:
return property.intValue.ToString();
case SerializedPropertyType.Character:
return property.intValue.ToString();
case SerializedPropertyType.AnimationCurve:
return property.animationCurveValue.ToString();
case SerializedPropertyType.Gradient:
return property.gradientValue.ToString();
case SerializedPropertyType.Vector3:
return property.vector3Value.ToString();
case SerializedPropertyType.Vector4:
return property.vector4Value.ToString();
case SerializedPropertyType.Vector2:
return property.vector2Value.ToString();
case SerializedPropertyType.Rect:
return property.rectValue.ToString();
case SerializedPropertyType.Bounds:
return property.boundsValue.ToString();
case SerializedPropertyType.Quaternion:
return property.quaternionValue.ToString();
case SerializedPropertyType.Generic:
return string.Empty;
default:
Debug.LogWarning("Unknown Property Type: " + property.propertyType);
return string.Empty;
}
}
// Is this a recordable clip on an animation track.
internal static bool IsRecordableAnimationClip(TimelineClip clip)
{
if (!clip.recordable)
return false;
AnimationPlayableAsset asset = clip.asset as AnimationPlayableAsset;
if (asset == null)
return false;
return true;
}
public static IList<PlayableDirector> GetSubTimelines(TimelineClip clip, IExposedPropertyTable director)
{
var editor = CustomTimelineEditorCache.GetClipEditor(clip);
List<PlayableDirector> directors = new List<PlayableDirector>();
try
{
editor.GetSubTimelines(clip, director as PlayableDirector, directors);
}
catch (Exception e)
{
Debug.LogException(e);
}
return directors;
}
public static bool IsAllSubTrackMuted(TrackAsset asset)
{
if (asset is GroupTrack)
return asset.mutedInHierarchy;
foreach (TrackAsset t in asset.GetChildTracks())
{
if (!t.muted)
return false;
var childMuted = IsAllSubTrackMuted(t);
if (!childMuted)
return false;
}
return true;
}
public static bool IsParentMuted(TrackAsset asset)
{
TrackAsset p = asset.parent as TrackAsset;
if (p == null) return false;
return p is GroupTrack ? p.mutedInHierarchy : IsParentMuted(p);
}
public static IEnumerable<PlayableDirector> GetAllDirectorsInHierarchy(PlayableDirector mainDirector)
{
var directors = new HashSet<PlayableDirector> { mainDirector };
GetAllDirectorsInHierarchy(mainDirector, directors);
return directors;
}
static void GetAllDirectorsInHierarchy(PlayableDirector director, ISet<PlayableDirector> directors)
{
var timelineAsset = director.playableAsset as TimelineAsset;
if (timelineAsset == null)
return;
foreach (var track in timelineAsset.GetOutputTracks())
{
foreach (var clip in track.clips)
{
foreach (var subDirector in GetSubTimelines(clip, director))
{
if (!directors.Contains(subDirector))
{
directors.Add(subDirector);
GetAllDirectorsInHierarchy(subDirector, directors);
}
}
}
}
}
public static IEnumerable<T> GetBindingsFromDirectors<T>(IEnumerable<PlayableDirector> directors) where T : Object
{
var bindings = new HashSet<T>();
foreach (var director in directors)
{
if (director.playableAsset == null) continue;
foreach (var output in director.playableAsset.outputs)
{
var binding = director.GetGenericBinding(output.sourceObject) as T;
if (binding != null)
bindings.Add(binding);
}
}
return bindings;
}
public static bool IsLockedFromGroup(TrackAsset asset)
{
TrackAsset p = asset.parent as TrackAsset;
if (p == null) return false;
return p is GroupTrack ? p.lockedInHierarchy : IsLockedFromGroup(p);
}
internal static bool IsCurrentSequenceValid()
{
return TimelineWindow.instance != null
&& TimelineWindow.instance.state != null
&& TimelineWindow.instance.state.editSequence != null;
}
}
}