AnimationPlayableAssetInspector.cs
14.3 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
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
[CustomEditor(typeof(AnimationPlayableAsset)), CanEditMultipleObjects]
class AnimationPlayableAssetInspector : Editor
{
static class Styles
{
public static readonly GUIContent RotationText = EditorGUIUtility.TrTextContent("Rotation");
public static readonly GUIContent AnimClipText = EditorGUIUtility.TrTextContent("Animation Clip");
public static readonly GUIContent TransformOffsetTitle = EditorGUIUtility.TrTextContent("Clip Transform Offsets", "Use this to offset the root transform position and rotation relative to the track when playing this clip");
public static readonly GUIContent AnimationClipName = EditorGUIUtility.TrTextContent("Animation Clip Name");
public static readonly GUIContent MatchTargetFieldsTitle = EditorGUIUtility.TrTextContent("Offsets Match Fields", "Fields to apply when matching offsets on clips. The defaults can be set on the track.");
public static readonly GUIContent UseDefaults = EditorGUIUtility.TrTextContent("Use defaults");
public static readonly GUIContent RemoveStartOffset = EditorGUIUtility.TrTextContent("Remove Start Offset", "Makes playback of the clip play relative to first key of the root transform");
public static readonly GUIContent ApplyFootIK = EditorGUIUtility.TrTextContent("Foot IK", "Enable to apply foot IK to the AnimationClip when the target is humanoid.");
public static readonly GUIContent Loop = EditorGUIUtility.TrTextContent("Loop", "Whether the source Animation Clip loops during playback.");
}
TimelineWindow m_TimelineWindow;
GameObject m_Binding;
TimelineAnimationUtilities.OffsetEditMode m_OffsetEditMode = TimelineAnimationUtilities.OffsetEditMode.None;
EditorClip m_EditorClip;
EditorClip[] m_EditorClips;
SerializedProperty m_PositionProperty;
SerializedProperty m_RotationProperty;
SerializedProperty m_AnimClipProperty;
SerializedProperty m_UseTrackMatchFieldsProperty;
SerializedProperty m_MatchTargetFieldsProperty;
SerializedObject m_SerializedAnimClip;
SerializedProperty m_SerializedAnimClipName;
SerializedProperty m_RemoveStartOffsetProperty;
SerializedProperty m_ApplyFootIK;
SerializedProperty m_Loop;
Vector3 m_LastPosition;
Vector3 m_LastRotation;
public override void OnInspectorGUI()
{
if (target == null)
return;
serializedObject.Update();
if (!m_TimelineWindow) m_TimelineWindow = TimelineWindow.instance;
ShowAnimationClipField();
ShowRecordableClipRename();
ShowAnimationClipWarnings();
EditorGUI.BeginChangeCheck();
TransformOffsetsGUI();
// extra checks are because the context menu may need to cause a re-evaluate
bool changed = EditorGUI.EndChangeCheck() ||
m_LastPosition != m_PositionProperty.vector3Value ||
m_LastRotation != m_RotationProperty.vector3Value;
m_LastPosition = m_PositionProperty.vector3Value;
m_LastRotation = m_RotationProperty.vector3Value;
if (changed)
{
// updates the changed properties and pushes them to the active playable
serializedObject.ApplyModifiedProperties();
((AnimationPlayableAsset)target).LiveLink();
// force an evaluate to happen next frame
if (TimelineWindow.instance != null && TimelineWindow.instance.state != null)
{
TimelineWindow.instance.state.Evaluate();
}
}
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_ApplyFootIK, Styles.ApplyFootIK);
EditorGUILayout.PropertyField(m_Loop, Styles.Loop);
if (EditorGUI.EndChangeCheck())
TimelineEditor.Refresh(RefreshReason.ContentsModified);
serializedObject.ApplyModifiedProperties();
}
void ShowAnimationClipField()
{
bool disabled = m_EditorClips == null || m_EditorClips.Any(c => c.clip == null || c.clip.recordable);
using (new EditorGUI.DisabledScope(disabled))
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_AnimClipProperty, Styles.AnimClipText);
if (EditorGUI.EndChangeCheck())
{
// rename the timeline clips to match the animation name if it did previously
if (m_AnimClipProperty.objectReferenceValue != null && m_EditorClips != null)
{
var newName = m_AnimClipProperty.objectReferenceValue.name;
foreach (var c in m_EditorClips)
{
if (c == null || c.clip == null || c.clip.asset == null)
continue;
var apa = c.clip.asset as AnimationPlayableAsset;
if (apa != null && apa.clip != null && c.clip.displayName == apa.clip.name)
{
if (c.clip.parentTrack != null)
Undo.RegisterCompleteObjectUndo(c.clip.parentTrack, "Inspector");
c.clip.displayName = newName;
}
}
}
TimelineEditor.Refresh(RefreshReason.ContentsModified);
}
}
}
void TransformOffsetsMatchFieldsGUI()
{
var rect = EditorGUILayout.GetControlRect(true);
EditorGUI.BeginProperty(rect, Styles.MatchTargetFieldsTitle, m_UseTrackMatchFieldsProperty);
rect = EditorGUI.PrefixLabel(rect, Styles.MatchTargetFieldsTitle);
int oldIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
EditorGUI.BeginChangeCheck();
bool val = m_UseTrackMatchFieldsProperty.boolValue;
val = EditorGUI.ToggleLeft(rect, Styles.UseDefaults, val);
if (EditorGUI.EndChangeCheck())
m_UseTrackMatchFieldsProperty.boolValue = val;
EditorGUI.indentLevel = oldIndent;
EditorGUI.EndProperty();
if (!val || m_UseTrackMatchFieldsProperty.hasMultipleDifferentValues)
{
EditorGUI.indentLevel++;
AnimationTrackInspector.MatchTargetsFieldGUI(m_MatchTargetFieldsProperty);
EditorGUI.indentLevel--;
}
}
void TransformOffsetsGUI()
{
if (ShouldShowOffsets())
{
EditorGUILayout.Space();
EditorGUILayout.LabelField(Styles.TransformOffsetTitle);
EditorGUI.indentLevel++;
using (new EditorGUI.DisabledScope(targets.Length > 1))
{
var previousOffsetMode = m_OffsetEditMode;
AnimationTrackInspector.ShowMotionOffsetEditModeToolbar(ref m_OffsetEditMode);
if (previousOffsetMode != m_OffsetEditMode)
{
SetTimeToClip();
SceneView.RepaintAll();
}
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(m_PositionProperty);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(m_RotationProperty, Styles.RotationText);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUI.indentLevel--;
TransformOffsetsMatchFieldsGUI();
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_RemoveStartOffsetProperty, Styles.RemoveStartOffset);
if (EditorGUI.EndChangeCheck())
{
TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
Repaint();
}
}
}
void Reevaluate()
{
if (m_TimelineWindow != null && m_TimelineWindow.state != null)
{
m_TimelineWindow.state.Refresh();
m_TimelineWindow.state.EvaluateImmediate();
}
}
// Make sure the director time is within the bounds of the clip
void SetTimeToClip()
{
if (m_TimelineWindow != null && m_TimelineWindow.state != null)
{
m_TimelineWindow.state.editSequence.time = Math.Min(m_EditorClip.clip.end, Math.Max(m_EditorClip.clip.start, m_TimelineWindow.state.editSequence.time));
}
}
public void OnEnable()
{
if (target == null) // case 946080
return;
m_EditorClip = UnityEditor.Selection.activeObject as EditorClip;
m_EditorClips = UnityEditor.Selection.objects.OfType<EditorClip>().ToArray();
SceneView.duringSceneGui += OnSceneGUI;
m_PositionProperty = serializedObject.FindProperty("m_Position");
m_PositionProperty.isExpanded = true;
m_RotationProperty = serializedObject.FindProperty("m_EulerAngles");
m_AnimClipProperty = serializedObject.FindProperty("m_Clip");
m_UseTrackMatchFieldsProperty = serializedObject.FindProperty("m_UseTrackMatchFields");
m_MatchTargetFieldsProperty = serializedObject.FindProperty("m_MatchTargetFields");
m_RemoveStartOffsetProperty = serializedObject.FindProperty("m_RemoveStartOffset");
m_ApplyFootIK = serializedObject.FindProperty("m_ApplyFootIK");
m_Loop = serializedObject.FindProperty("m_Loop");
m_LastPosition = m_PositionProperty.vector3Value;
m_LastRotation = m_RotationProperty.vector3Value;
}
void OnDestroy()
{
SceneView.duringSceneGui -= OnSceneGUI;
}
void OnSceneGUI(SceneView sceneView)
{
DoManipulators();
}
Transform GetTransform()
{
if (m_Binding != null)
return m_Binding.transform;
if (m_TimelineWindow != null && m_TimelineWindow.state != null &&
m_TimelineWindow.state.editSequence.director != null &&
m_EditorClip != null && m_EditorClip.clip != null)
{
var obj = TimelineUtility.GetSceneGameObject(m_TimelineWindow.state.editSequence.director,
m_EditorClip.clip.parentTrack);
m_Binding = obj;
if (obj != null)
return obj.transform;
}
return null;
}
void DoManipulators()
{
if (m_EditorClip == null || m_EditorClip.clip == null)
return;
AnimationPlayableAsset animationPlayable = m_EditorClip.clip.asset as AnimationPlayableAsset;
AnimationTrack track = m_EditorClip.clip.parentTrack as AnimationTrack;
Transform transform = GetTransform();
if (transform != null && animationPlayable != null && m_OffsetEditMode != TimelineAnimationUtilities.OffsetEditMode.None && track != null)
{
UndoExtensions.RegisterPlayableAsset(animationPlayable, "Inspector");
Vector3 position = transform.position;
Quaternion rotation = transform.rotation;
EditorGUI.BeginChangeCheck();
if (m_OffsetEditMode == TimelineAnimationUtilities.OffsetEditMode.Translation)
{
position = Handles.PositionHandle(position, Tools.pivotRotation == PivotRotation.Global ? Quaternion.identity : rotation);
}
else if (m_OffsetEditMode == TimelineAnimationUtilities.OffsetEditMode.Rotation)
{
rotation = Handles.RotationHandle(rotation, position);
}
if (EditorGUI.EndChangeCheck())
{
var res = TimelineAnimationUtilities.UpdateClipOffsets(animationPlayable, track, transform, position, rotation);
animationPlayable.position = res.position;
animationPlayable.eulerAngles = AnimationUtility.GetClosestEuler(res.rotation, animationPlayable.eulerAngles, RotationOrder.OrderZXY);
Reevaluate();
Repaint();
}
}
}
void ShowAnimationClipWarnings()
{
AnimationClip clip = m_AnimClipProperty.objectReferenceValue as AnimationClip;
if (clip == null)
{
EditorGUILayout.HelpBox(AnimationPlayableAssetEditor.k_NoClipAssignedError, MessageType.Warning);
}
else if (clip.legacy)
{
EditorGUILayout.HelpBox(AnimationPlayableAssetEditor.k_LegacyClipError, MessageType.Warning);
}
}
bool ShouldShowOffsets()
{
return targets.OfType<AnimationPlayableAsset>().All(x => x.hasRootTransforms);
}
void ShowRecordableClipRename()
{
if (targets.Length > 1 || m_EditorClip == null || m_EditorClip.clip == null || !m_EditorClip.clip.recordable)
return;
AnimationClip clip = m_AnimClipProperty.objectReferenceValue as AnimationClip;
if (clip == null || !AssetDatabase.IsSubAsset(clip))
return;
if (m_SerializedAnimClip == null)
{
m_SerializedAnimClip = new SerializedObject(clip);
m_SerializedAnimClipName = m_SerializedAnimClip.FindProperty("m_Name");
}
if (m_SerializedAnimClipName != null)
{
m_SerializedAnimClip.Update();
EditorGUI.BeginChangeCheck();
EditorGUILayout.DelayedTextField(m_SerializedAnimClipName, Styles.AnimationClipName);
if (EditorGUI.EndChangeCheck())
m_SerializedAnimClip.ApplyModifiedProperties();
}
}
}
}