AnimationTrackInspector.cs
21.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//#define PERF_PROFILE
using System;
using System.ComponentModel;
using System.Linq;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
namespace UnityEditor.Timeline
{
[CustomEditor(typeof(AnimationTrack)), CanEditMultipleObjects]
class AnimationTrackInspector : TrackAssetInspector
{
static class Styles
{
public static GUIContent MatchTargetFieldsTitle = EditorGUIUtility.TrTextContent("Default Offset Match Fields", "Fields to apply when matching offsets on clips. These are the defaults, and can be overridden for each clip.");
public static readonly GUIContent PositionIcon = EditorGUIUtility.IconContent("MoveTool");
public static readonly GUIContent RotationIcon = EditorGUIUtility.IconContent("RotateTool");
public static GUIContent XTitle = EditorGUIUtility.TextContent("X");
public static GUIContent YTitle = EditorGUIUtility.TextContent("Y");
public static GUIContent ZTitle = EditorGUIUtility.TextContent("Z");
public static GUIContent PositionTitle = EditorGUIUtility.TrTextContent("Position");
public static GUIContent RotationTitle = EditorGUIUtility.TrTextContent("Rotation");
public static readonly GUIContent OffsetModeTitle = EditorGUIUtility.TrTextContent("Track Offsets");
public static readonly string TransformOffsetInfo = L10n.Tr("Transform offsets are applied to the entire track. Use this mode to play the animation track at a fixed position and rotation.");
public static readonly string SceneOffsetInfo = L10n.Tr("Scene offsets will use the existing transform as initial offsets. Use this to play the track from the gameObjects current position and rotation.");
public static readonly string AutoOffsetInfo = L10n.Tr("Auto will apply scene offsets if there is a controller attached to the animator and transform offsets otherwise.");
public static readonly string AutoOffsetWarning = L10n.Tr("This mode is deprecated may be removed in a future release.");
public static readonly string InheritedFromParent = L10n.Tr("Inherited");
public static readonly string InheritedToolTip = L10n.Tr("This value is inherited from it's parent track.");
public static readonly string AvatarMaskWarning = L10n.Tr("Applying an Avatar Mask to the base track may not properly mask Root Motion or Humanoid bones from an Animator Controller or other Timeline track.");
public static readonly GUIContent RecordingOffsets = EditorGUIUtility.TrTextContent("Recorded Offsets", "Offsets applied to recorded position and rotation keys");
public static readonly GUIContent RecordingIkApplied = EditorGUIUtility.TrTextContent("Apply Foot IK", "Applies Foot IK to recorded Animation.");
public static readonly GUIContent[] OffsetContents;
public static readonly GUIContent[] OffsetInheritContents;
static Styles()
{
var values = Enum.GetValues(typeof(TrackOffset));
OffsetContents = new GUIContent[values.Length];
OffsetInheritContents = new GUIContent[values.Length];
for (var index = 0; index < values.Length; index++)
{
var offset = (TrackOffset)index;
var name = ObjectNames.NicifyVariableName(L10n.Tr(offset.ToString()));
var memInfo = typeof(TrackOffset).GetMember(offset.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
name = ((DescriptionAttribute)attributes[0]).Description;
}
OffsetContents[index] = new GUIContent(name);
OffsetInheritContents[index] = new GUIContent(string.Format("{0} ({1})", InheritedFromParent, name));
}
}
}
TimelineAnimationUtilities.OffsetEditMode m_OffsetEditMode = TimelineAnimationUtilities.OffsetEditMode.None;
SerializedProperty m_MatchFieldsProperty;
SerializedProperty m_TrackPositionProperty;
SerializedProperty m_TrackRotationProperty;
SerializedProperty m_AvatarMaskProperty;
SerializedProperty m_ApplyAvatarMaskProperty;
SerializedProperty m_TrackOffsetProperty;
SerializedProperty m_RecordedOffsetPositionProperty;
SerializedProperty m_RecordedOffsetEulerProperty;
SerializedProperty m_RecordedApplyFootIK;
Vector3 m_lastPosition;
Vector3 m_lastRotation;
GUIContent m_TempContent = new GUIContent();
void Evaluate()
{
if (timelineWindow.state != null && timelineWindow.state.editSequence.director != null)
{
// force the update immediately, the deferred doesn't always work with the inspector
timelineWindow.state.editSequence.director.Evaluate();
}
}
void RebuildGraph()
{
TimelineEditor.Refresh(RefreshReason.ContentsModified);
}
public override void OnInspectorGUI()
{
using (new EditorGUI.DisabledScope(IsTrackLocked()))
{
serializedObject.Update();
DrawRootTransformOffset();
EditorGUI.BeginChangeCheck();
DrawRecordedProperties();
DrawAvatarProperties();
if (EditorGUI.EndChangeCheck())
RebuildGraph();
DrawMatchFieldsGUI();
serializedObject.ApplyModifiedProperties();
}
}
bool AnimatesRootTransform()
{
return targets.OfType<AnimationTrack>().All(t => t.AnimatesRootTransform());
}
bool ShouldDrawOffsets()
{
bool hasMultiple;
var offsetMode = GetOffsetMode(out hasMultiple);
if (hasMultiple)
return false;
if (offsetMode == TrackOffset.ApplySceneOffsets)
return false;
if (offsetMode == TrackOffset.ApplyTransformOffsets)
return true;
// Auto mode.
PlayableDirector director = this.m_Context as PlayableDirector;
if (director == null)
return false;
// If any bound animators have controllers don't show
foreach (var track in targets.OfType<AnimationTrack>())
{
var animator = track.GetBinding(director);
if (animator != null && animator.runtimeAnimatorController != null)
return false;
}
return true;
}
void DrawRootTransformOffset()
{
if (!AnimatesRootTransform())
return;
bool showWarning = SetupOffsetTooltip();
DrawRootTransformDropDown();
if (ShouldDrawOffsets())
{
EditorGUI.indentLevel++;
DrawRootMotionToolBar();
DrawRootMotionOffsetFields();
EditorGUI.indentLevel--;
}
if (showWarning)
{
EditorGUI.indentLevel++;
EditorGUILayout.HelpBox(Styles.AutoOffsetWarning, MessageType.Warning, true);
EditorGUI.indentLevel--;
}
}
bool SetupOffsetTooltip()
{
Styles.OffsetModeTitle.tooltip = string.Empty;
bool hasMultiple;
var offsetMode = GetOffsetMode(out hasMultiple);
bool showWarning = false;
if (!hasMultiple)
{
if (offsetMode == TrackOffset.ApplyTransformOffsets)
Styles.OffsetModeTitle.tooltip = Styles.TransformOffsetInfo;
else if (offsetMode == TrackOffset.ApplySceneOffsets)
Styles.OffsetModeTitle.tooltip = Styles.SceneOffsetInfo;
else if (offsetMode == TrackOffset.Auto)
{
Styles.OffsetModeTitle.tooltip = Styles.AutoOffsetInfo;
showWarning = true;
}
}
return showWarning;
}
void DrawRootTransformDropDown()
{
bool anySubTracks = targets.OfType<AnimationTrack>().Any(t => t.isSubTrack);
bool allSubTracks = targets.OfType<AnimationTrack>().All(t => t.isSubTrack);
bool mixed;
var rootOffsetMode = GetOffsetMode(out mixed);
// if we are showing subtracks, we need to show the current mode from the parent
// BUT keep it disabled
if (anySubTracks)
{
m_TempContent.tooltip = string.Empty;
if (mixed)
m_TempContent.text = EditorGUI.mixedValueContent.text;
else if (!allSubTracks)
m_TempContent.text = Styles.OffsetContents[(int)rootOffsetMode].text;
else
{
m_TempContent.text = Styles.OffsetInheritContents[(int)rootOffsetMode].text;
m_TempContent.tooltip = Styles.InheritedToolTip;
}
using (new EditorGUI.DisabledScope(true))
EditorGUILayout.LabelField(Styles.OffsetModeTitle, m_TempContent, EditorStyles.popup);
}
else
{
// We use an enum popup explicitly because it will handle the description attribute on the enum
using (new GUIMixedValueScope(mixed))
{
var rect = EditorGUILayout.GetControlRect(true, EditorGUI.kSingleLineHeight);
EditorGUI.BeginProperty(rect, Styles.OffsetModeTitle, m_TrackOffsetProperty);
EditorGUI.BeginChangeCheck();
var result = (TrackOffset)EditorGUI.EnumPopup(rect, Styles.OffsetModeTitle, (TrackOffset)m_TrackOffsetProperty.intValue);
if (EditorGUI.EndChangeCheck())
{
m_TrackOffsetProperty.enumValueIndex = (int)result;
// this property changes the recordable state of the objects, so auto disable recording
if (TimelineWindow.instance != null)
{
if (TimelineWindow.instance.state != null)
TimelineWindow.instance.state.recording = false;
RebuildGraph();
}
}
EditorGUI.EndProperty();
}
}
}
void DrawMatchFieldsGUI()
{
if (!AnimatesRootTransform())
return;
m_MatchFieldsProperty.isExpanded = EditorGUILayout.Foldout(m_MatchFieldsProperty.isExpanded, Styles.MatchTargetFieldsTitle, true);
if (m_MatchFieldsProperty.isExpanded)
{
EditorGUI.indentLevel++;
MatchTargetsFieldGUI(m_MatchFieldsProperty);
EditorGUI.indentLevel--;
}
}
void DrawRootMotionOffsetFields()
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(m_TrackPositionProperty);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(m_TrackRotationProperty, Styles.RotationTitle);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.Space();
if (EditorGUI.EndChangeCheck())
{
UpdateOffsets();
}
}
void DrawRootMotionToolBar()
{
bool disable = targets.Length > 1;
bool changed = false;
if (!disable)
{
// detects external changes
changed |= m_lastPosition != m_TrackPositionProperty.vector3Value || m_lastRotation != m_TrackRotationProperty.vector3Value;
m_lastPosition = m_TrackPositionProperty.vector3Value;
m_lastRotation = m_TrackRotationProperty.vector3Value;
SceneView.RepaintAll();
}
EditorGUI.BeginChangeCheck();
using (new EditorGUI.DisabledScope(disable))
ShowMotionOffsetEditModeToolbar(ref m_OffsetEditMode);
changed |= EditorGUI.EndChangeCheck();
if (changed)
{
UpdateOffsets();
}
}
void UpdateOffsets()
{
foreach (var track in targets.OfType<AnimationTrack>())
track.UpdateClipOffsets();
Evaluate();
}
void DrawAvatarProperties()
{
EditorGUILayout.PropertyField(m_ApplyAvatarMaskProperty);
if (m_ApplyAvatarMaskProperty.hasMultipleDifferentValues || m_ApplyAvatarMaskProperty.boolValue)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_AvatarMaskProperty);
EditorGUI.indentLevel--;
}
if (targets.OfType<AnimationTrack>().Any(x => !x.isSubTrack))
EditorGUILayout.HelpBox(Styles.AvatarMaskWarning, MessageType.Warning);
EditorGUILayout.Space();
}
public static void ShowMotionOffsetEditModeToolbar(ref TimelineAnimationUtilities.OffsetEditMode motionOffset)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.FlexibleSpace();
int newMotionOffsetMode = GUILayout.Toolbar((int)motionOffset, new[] { Styles.PositionIcon, Styles.RotationIcon });
if (GUI.changed)
{
if ((int)motionOffset == newMotionOffsetMode) //untoggle the button
motionOffset = TimelineAnimationUtilities.OffsetEditMode.None;
else
motionOffset = (TimelineAnimationUtilities.OffsetEditMode)newMotionOffsetMode;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.Space(3);
}
public override void OnEnable()
{
base.OnEnable();
SceneView.duringSceneGui += OnSceneGUI;
m_MatchFieldsProperty = serializedObject.FindProperty("m_MatchTargetFields");
m_TrackPositionProperty = serializedObject.FindProperty("m_Position");
m_TrackRotationProperty = serializedObject.FindProperty("m_EulerAngles");
m_TrackOffsetProperty = serializedObject.FindProperty("m_TrackOffset");
m_AvatarMaskProperty = serializedObject.FindProperty("m_AvatarMask");
m_ApplyAvatarMaskProperty = serializedObject.FindProperty("m_ApplyAvatarMask");
m_RecordedOffsetPositionProperty = serializedObject.FindProperty("m_InfiniteClipOffsetPosition");
m_RecordedOffsetEulerProperty = serializedObject.FindProperty("m_InfiniteClipOffsetEulerAngles");
m_RecordedApplyFootIK = serializedObject.FindProperty("m_InfiniteClipApplyFootIK");
m_lastPosition = m_TrackPositionProperty.vector3Value;
m_lastRotation = m_TrackRotationProperty.vector3Value;
}
public void OnDestroy()
{
SceneView.duringSceneGui -= OnSceneGUI;
}
void OnSceneGUI(SceneView sceneView)
{
DoOffsetManipulator();
}
void DoOffsetManipulator()
{
if (targets.Length > 1) //do not edit the track offset on a multiple selection
return;
if (timelineWindow == null || timelineWindow.state == null || timelineWindow.state.editSequence.director == null)
return;
AnimationTrack animationTrack = target as AnimationTrack;
if (animationTrack != null && (animationTrack.trackOffset == TrackOffset.ApplyTransformOffsets) && m_OffsetEditMode != TimelineAnimationUtilities.OffsetEditMode.None)
{
var boundObject = TimelineUtility.GetSceneGameObject(timelineWindow.state.editSequence.director, animationTrack);
var boundObjectTransform = boundObject != null ? boundObject.transform : null;
var offsets = TimelineAnimationUtilities.GetTrackOffsets(animationTrack, boundObjectTransform);
EditorGUI.BeginChangeCheck();
switch (m_OffsetEditMode)
{
case TimelineAnimationUtilities.OffsetEditMode.Translation:
offsets.position = Handles.PositionHandle(offsets.position, (Tools.pivotRotation == PivotRotation.Global)
? Quaternion.identity
: offsets.rotation);
break;
case TimelineAnimationUtilities.OffsetEditMode.Rotation:
offsets.rotation = Handles.RotationHandle(offsets.rotation, offsets.position);
break;
}
if (EditorGUI.EndChangeCheck())
{
UndoExtensions.RegisterTrack(animationTrack, "Inspector");
TimelineAnimationUtilities.UpdateTrackOffset(animationTrack, boundObjectTransform, offsets);
Evaluate();
Repaint();
}
}
}
public void DrawRecordedProperties()
{
// only show if this applies to all targets
foreach (var track in targets)
{
var animationTrack = track as AnimationTrack;
if (animationTrack == null || animationTrack.inClipMode || animationTrack.infiniteClip == null || animationTrack.infiniteClip.empty)
return;
}
GUILayout.Label(Styles.RecordingOffsets);
EditorGUI.indentLevel++;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(m_RecordedOffsetPositionProperty, Styles.PositionTitle);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(m_RecordedOffsetEulerProperty, Styles.RotationTitle);
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel--;
EditorGUILayout.Space();
EditorGUILayout.PropertyField(m_RecordedApplyFootIK, Styles.RecordingIkApplied);
EditorGUILayout.Space();
}
public static void MatchTargetsFieldGUI(SerializedProperty property)
{
const float ToggleWidth = 20;
int value = 0;
MatchTargetFields enumValue = (MatchTargetFields)property.intValue;
EditorGUI.BeginChangeCheck();
Rect rect = EditorGUILayout.GetControlRect(false, kLineHeight * 2);
Rect itemRect = new Rect(rect.x, rect.y, rect.width, kLineHeight);
EditorGUI.BeginProperty(rect, Styles.MatchTargetFieldsTitle, property);
float minWidth = 0, maxWidth = 0;
EditorStyles.label.CalcMinMaxWidth(Styles.XTitle, out minWidth, out maxWidth);
float width = minWidth + ToggleWidth;
GUILayout.BeginHorizontal();
Rect r = EditorGUI.PrefixLabel(itemRect, Styles.PositionTitle);
int oldIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
r.width = width;
value |= EditorGUI.ToggleLeft(r, Styles.XTitle, enumValue.HasAny(MatchTargetFields.PositionX)) ? (int)MatchTargetFields.PositionX : 0;
r.x += width;
value |= EditorGUI.ToggleLeft(r, Styles.YTitle, enumValue.HasAny(MatchTargetFields.PositionY)) ? (int)MatchTargetFields.PositionY : 0;
r.x += width;
value |= EditorGUI.ToggleLeft(r, Styles.ZTitle, enumValue.HasAny(MatchTargetFields.PositionZ)) ? (int)MatchTargetFields.PositionZ : 0;
EditorGUI.indentLevel = oldIndent;
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
itemRect.y += kLineHeight;
r = EditorGUI.PrefixLabel(itemRect, Styles.RotationTitle);
EditorGUI.indentLevel = 0;
r.width = width;
value |= EditorGUI.ToggleLeft(r, Styles.XTitle, enumValue.HasAny(MatchTargetFields.RotationX)) ? (int)MatchTargetFields.RotationX : 0;
r.x += width;
value |= EditorGUI.ToggleLeft(r, Styles.YTitle, enumValue.HasAny(MatchTargetFields.RotationY)) ? (int)MatchTargetFields.RotationY : 0;
r.x += width;
value |= EditorGUI.ToggleLeft(r, Styles.ZTitle, enumValue.HasAny(MatchTargetFields.RotationZ)) ? (int)MatchTargetFields.RotationZ : 0;
EditorGUI.indentLevel = oldIndent;
GUILayout.EndHorizontal();
EditorGUI.EndProperty();
if (EditorGUI.EndChangeCheck())
{
property.intValue = value;
}
}
static TrackOffset GetOffsetMode(AnimationTrack track)
{
if (track.isSubTrack)
{
var parent = track.parent as AnimationTrack;
if (parent != null) // fallback to the current track if there is an error
track = parent;
}
return track.trackOffset;
}
// gets the current mode,
TrackOffset GetOffsetMode(out bool hasMultiple)
{
var rootOffsetMode = GetOffsetMode(target as AnimationTrack);
hasMultiple = targets.OfType<AnimationTrack>().Any(t => GetOffsetMode(t) != rootOffsetMode);
return rootOffsetMode;
}
}
}