/
AnimatorController.cs
349 lines (287 loc) · 12.3 KB
/
AnimatorController.cs
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using RequiredByNativeCodeAttribute = UnityEngine.Scripting.RequiredByNativeCodeAttribute;
namespace UnityEditor.Animations
{
[NativeClass(null)]
public sealed partial class AnimatorController : RuntimeAnimatorController
{
internal System.Action OnAnimatorControllerDirty;
internal static AnimatorController lastActiveController = null;
internal static int lastActiveLayerIndex = 0;
private const string kControllerExtension = "controller";
internal PushUndoIfNeeded undoHandler = new PushUndoIfNeeded(true);
internal bool pushUndo { set { undoHandler.pushUndo = value; } }
internal string GetDefaultBlendTreeParameter()
{
for (int i = 0; i < parameters.Length; i++)
{
if (parameters[i].type == AnimatorControllerParameterType.Float)
return parameters[i].name;
}
AddParameter("Blend", AnimatorControllerParameterType.Float);
return "Blend";
}
[RequiredByNativeCode]
internal static void OnInvalidateAnimatorController(AnimatorController controller)
{
if (controller.OnAnimatorControllerDirty != null)
controller.OnAnimatorControllerDirty();
}
internal AnimatorStateMachine FindEffectiveRootStateMachine(int layerIndex)
{
AnimatorControllerLayer currentLayer = layers[layerIndex];
while (currentLayer.syncedLayerIndex != -1)
{
currentLayer = layers[currentLayer.syncedLayerIndex];
}
return currentLayer.stateMachine;
}
public void AddLayer(string name)
{
AnimatorControllerLayer newLayer = new AnimatorControllerLayer();
newLayer.name = MakeUniqueLayerName(name);
newLayer.stateMachine = new AnimatorStateMachine();
newLayer.stateMachine.name = newLayer.name;
newLayer.stateMachine.hideFlags = HideFlags.HideInHierarchy;
if (AssetDatabase.GetAssetPath(this) != "")
AssetDatabase.AddObjectToAsset(newLayer.stateMachine, AssetDatabase.GetAssetPath(this));
undoHandler.DoUndoCreated(newLayer.stateMachine, "Layer added");
AddLayer(newLayer);
}
public void AddLayer(AnimatorControllerLayer layer)
{
undoHandler.DoUndo(this, "Layer added");
AnimatorControllerLayer[] layerVector = layers;
ArrayUtility.Add(ref layerVector, layer);
layers = layerVector;
}
internal void RemoveLayers(List<int> layerIndexes)
{
undoHandler.DoUndo(this, "Layers removed");
AnimatorControllerLayer[] layerVector = this.layers;
foreach (var layerIndex in layerIndexes)
{
for (var i = 0; i < layerVector.Length; ++i)
{
var syncedLayerIndex = layerVector[i].syncedLayerIndex;
if (syncedLayerIndex > layerIndex)
{
// synced layer is after the layer being removed, so it's going to be shifted upon removal
layerVector[i].syncedLayerIndex = syncedLayerIndex - 1;
}
}
RemoveLayerInternal(layerIndex, ref layerVector);
}
this.layers = layerVector;
}
private void RemoveLayerInternal(int index, ref AnimatorControllerLayer[] layerVector)
{
if (layerVector[index].syncedLayerIndex == -1 && layerVector[index].stateMachine != null)
{
undoHandler.DoUndo(layerVector[index].stateMachine, "Layer removed");
layerVector[index].stateMachine.Clear();
if (MecanimUtilities.AreSameAsset(this, layerVector[index].stateMachine))
Undo.DestroyObjectImmediate(layerVector[index].stateMachine);
}
ArrayUtility.Remove(ref layerVector, layerVector[index]);
}
public void RemoveLayer(int index)
{
undoHandler.DoUndo(this, "Layer removed");
AnimatorControllerLayer[] layerVector = layers;
RemoveLayerInternal(index, ref layerVector);
layers = layerVector;
}
internal bool IsStateInLayer(AnimatorState state, int layerIndex)
{
if (layerIndex >= layers.Length)
{
return false;
}
return layers[layerIndex].stateMachine.HasState(state);
}
internal int GetStateLayer(AnimatorState state)
{
for (int i = 0; i < layers.Length; ++i)
{
if (layers[i].stateMachine.HasState(state))
{
return i;
}
}
return -1;
}
public void AddParameter(string name, AnimatorControllerParameterType type)
{
AnimatorControllerParameter newParameter = new AnimatorControllerParameter();
newParameter.name = MakeUniqueParameterName(name);
newParameter.type = type;
AddParameter(newParameter);
}
public void AddParameter(AnimatorControllerParameter paramater)
{
undoHandler.DoUndo(this, "Parameter added");
AnimatorControllerParameter[] parameterVector = parameters;
ArrayUtility.Add(ref parameterVector, paramater);
parameters = parameterVector;
}
public void RemoveParameter(int index)
{
undoHandler.DoUndo(this, "Parameter removed");
AnimatorControllerParameter[] parameterVector = parameters;
ArrayUtility.Remove(ref parameterVector, parameterVector[index]);
parameters = parameterVector;
}
public void RemoveParameter(AnimatorControllerParameter parameter)
{
undoHandler.DoUndo(this, "Parameter removed");
AnimatorControllerParameter[] parameterVector = parameters;
ArrayUtility.Remove(ref parameterVector, parameter);
parameters = parameterVector;
}
// We cannot call AddMotion from native code, because there are multiple signatures of that method, creating ambiguity.
[RequiredByNativeCode]
private AnimatorState AddMotionInternal(Motion motion)
{
return AddMotion(motion);
}
public AnimatorState AddMotion(Motion motion)
{
return AddMotion(motion, 0);
}
public AnimatorState AddMotion(Motion motion, int layerIndex)
{
AnimatorState state = layers[layerIndex].stateMachine.AddState(motion.name);
state.motion = motion;
return state;
}
public AnimatorState CreateBlendTreeInController(string name, out BlendTree tree)
{
return CreateBlendTreeInController(name, out tree, 0);
}
public AnimatorState CreateBlendTreeInController(string name, out BlendTree tree, int layerIndex)
{
tree = new BlendTree();
tree.name = name;
tree.blendParameter = tree.blendParameterY = GetDefaultBlendTreeParameter();
if (AssetDatabase.GetAssetPath(this) != "")
AssetDatabase.AddObjectToAsset(tree, AssetDatabase.GetAssetPath(this));
undoHandler.DoUndoCreated(tree, "Blend Tree Created");
AnimatorState state = layers[layerIndex].stateMachine.AddState(tree.name);
state.motion = tree;
return state;
}
public static AnimatorController CreateAnimatorControllerAtPath(string path)
{
AnimatorController controller = new AnimatorController();
controller.name = Path.GetFileName(path);
AssetDatabase.CreateAsset(controller, path);
controller.pushUndo = false;
controller.AddLayer("Base Layer");
controller.pushUndo = true;
return controller;
}
public static AnimationClip AllocateAnimatorClip(string name)
{
var clip = UnityEditorInternal.AnimationWindowUtility.AllocateAndSetupClip(true);
clip.name = name;
return clip;
}
[RequiredByNativeCode]
internal static AnimatorController CreateAnimatorControllerForClip(AnimationClip clip, GameObject animatedObject)
{
string path = AssetDatabase.GetAssetPath(clip);
if (string.IsNullOrEmpty(path))
return null;
string name = System.Text.RegularExpressions.Regex.Replace(animatedObject.name, @"[\\\./]", "_");
path = Path.Combine(FileUtil.DeleteLastPathNameComponent(path), name + "." + kControllerExtension);
path = AssetDatabase.GenerateUniqueAssetPath(path);
if (string.IsNullOrEmpty(path))
return null;
return CreateAnimatorControllerAtPathWithClip(path, clip);
}
public static AnimatorController CreateAnimatorControllerAtPathWithClip(string path, AnimationClip clip)
{
AnimatorController controller = CreateAnimatorControllerAtPath(path);
AnimatorStateMachine stateMachine = controller.layers[0].stateMachine;
stateMachine.pushUndo = false;
var state = stateMachine.AddState(clip.name);
state.motion = clip;
stateMachine.pushUndo = true;
return controller;
}
public void SetStateEffectiveMotion(AnimatorState state, Motion motion)
{
SetStateEffectiveMotion(state, motion, 0);
}
public void SetStateEffectiveMotion(AnimatorState state, Motion motion, int layerIndex)
{
//delete existing nested blend tree asset
Motion selectedMotion = GetStateEffectiveMotion(state, layerIndex);
BlendTree blendTree = selectedMotion as BlendTree;
if (blendTree != null && !AssetDatabase.IsMainAsset(blendTree))
{
MecanimUtilities.DestroyBlendTreeRecursive(blendTree);
}
if (layers[layerIndex].syncedLayerIndex == -1)
{
undoHandler.DoUndo(state, "Set Motion");
state.motion = motion;
}
else
{
undoHandler.DoUndo(this, "Set Motion");
AnimatorControllerLayer[] layerArray = layers;
layerArray[layerIndex].SetOverrideMotion(state, motion);
layers = layerArray;
}
}
public Motion GetStateEffectiveMotion(AnimatorState state)
{
return GetStateEffectiveMotion(state, 0);
}
public Motion GetStateEffectiveMotion(AnimatorState state, int layerIndex)
{
if (layers[layerIndex].syncedLayerIndex == -1)
{
return state.motion;
}
else
{
return layers[layerIndex].GetOverrideMotion(state);
}
}
public void SetStateEffectiveBehaviours(AnimatorState state, int layerIndex, StateMachineBehaviour[] behaviours)
{
if (layers[layerIndex].syncedLayerIndex == -1)
{
undoHandler.DoUndo(state, "Set Behaviours");
state.behaviours = behaviours;
}
else
{
undoHandler.DoUndo(this, "Set Behaviours");
Internal_SetEffectiveBehaviours(state, layerIndex, behaviours);
}
}
public StateMachineBehaviour[] GetStateEffectiveBehaviours(AnimatorState state, int layerIndex)
{
return Internal_GetEffectiveBehaviours(state, layerIndex) as StateMachineBehaviour[];
}
[System.Obsolete("parameterCount is obsolete. Use parameters.Length instead.", true)]
int parameterCount
{
get { return 0; }
}
[System.Obsolete("layerCount is obsolete. Use layers.Length instead.", true)]
int layerCount
{
get { return 0; }
}
}
}