TimelineCreateUtilities.cs
4.37 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
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Timeline
{
static class TimelineCreateUtilities
{
// based off of ObjectNames.GetUniqueName, but can exist in runtime
public static string GenerateUniqueActorName(List<ScriptableObject> tracks, string name)
{
if (!tracks.Exists(x => ((object)x) != null && x.name == name))
return name;
int numberInParentheses = 0;
string baseName = name;
if (!string.IsNullOrEmpty(name) && name[name.Length - 1] == ')')
{
int index = name.LastIndexOf('(');
if (index > 0)
{
string numberString = name.Substring(index + 1, name.Length - index - 2);
if (int.TryParse(numberString, out numberInParentheses))
{
numberInParentheses++;
baseName = name.Substring(0, index);
}
}
}
baseName = baseName.TrimEnd();
for (int i = numberInParentheses; i < numberInParentheses + 5000; i++)
{
if (i > 0)
{
string result = string.Format("{0} ({1})", baseName, i);
if (!tracks.Exists(x => ((object)x) != null && x.name == result))
return result;
}
}
// Fallback
return name;
}
public static void SaveAssetIntoObject(Object childAsset, Object masterAsset)
{
if (childAsset == null || masterAsset == null)
return;
if ((masterAsset.hideFlags & HideFlags.DontSave) != 0)
{
childAsset.hideFlags |= HideFlags.DontSave;
}
else
{
childAsset.hideFlags |= HideFlags.HideInHierarchy;
#if UNITY_EDITOR
if (!AssetDatabase.Contains(childAsset) && AssetDatabase.Contains(masterAsset))
AssetDatabase.AddObjectToAsset(childAsset, masterAsset);
#endif
}
}
public static AnimationClip CreateAnimationClipForTrack(string name, TrackAsset track, bool isLegacy)
{
var timelineAsset = track != null ? track.timelineAsset : null;
var trackFlags = track != null ? track.hideFlags : HideFlags.None;
var curves = new AnimationClip
{
legacy = isLegacy,
name = name,
frameRate = timelineAsset == null
? TimelineAsset.EditorSettings.kDefaultFps
: timelineAsset.editorSettings.fps
};
SaveAssetIntoObject(curves, timelineAsset);
curves.hideFlags = trackFlags & ~HideFlags.HideInHierarchy; // Never hide in hierarchy
TimelineUndo.RegisterCreatedObjectUndo(curves, "Create Curves");
return curves;
}
public static bool ValidateParentTrack(TrackAsset parent, Type childType)
{
if (childType == null || !typeof(TrackAsset).IsAssignableFrom(childType))
return false;
// no parent is valid for any type
if (parent == null)
return true;
// A track supports layers if it implements ILayerable. Only supported for parents that are
// the same exact type as the child class, and 1 level of nesting only
if (parent is ILayerable && !parent.isSubTrack && parent.GetType() == childType)
return true;
var attr = Attribute.GetCustomAttribute(parent.GetType(), typeof(SupportsChildTracksAttribute)) as SupportsChildTracksAttribute;
if (attr == null)
return false;
// group track case, accepts all
if (attr.childType == null)
return true;
// specific case. Specifies nesting level
if (childType == attr.childType)
{
int nestCount = 0;
var p = parent;
while (p != null && p.isSubTrack)
{
nestCount++;
p = p.parent as TrackAsset;
}
return nestCount < attr.levels;
}
return false;
}
}
}