forked from s-m-k/Unity-Animation-Hierarchy-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationHierarchyEditor.cs
275 lines (215 loc) · 9.5 KB
/
AnimationHierarchyEditor.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
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class AnimationHierarchyEditor : EditorWindow {
#region Automated Variables
private readonly Dictionary<string, List<EditorCurveBinding>> paths = new Dictionary<string, List<EditorCurveBinding>>();
private readonly List<string> pathsKeys = new List<string>();
private static GUIContent resetIcon;
private AnimationClip[] animationClips;
private Vector2 scrollPos = Vector2.zero;
#endregion
#region Input Variables
private string[] tempPathOverrides;
private Animator animatorObject;
private bool regexReplace;
private string oldPathValue = "Root";
private string newPathValue = "SomeNewObject/Root";
#endregion
[MenuItem("Window/Animation Hierarchy Editor")]
static void ShowWindow() => GetWindow<AnimationHierarchyEditor>();
void OnSelectionChange()
{
animationClips = Selection.GetFiltered<AnimationClip>(SelectionMode.Assets);
FillModel();
Repaint();
}
void OnGUI()
{
if (animationClips == null || animationClips.Length == 0)
{
DrawTitle("Please select an Animation Clip");
return;
}
scrollPos = GUILayout.BeginScrollView(scrollPos, GUIStyle.none);
using (new GUILayout.VerticalScope("helpbox"))
{
animatorObject = (Animator) EditorGUILayout.ObjectField("Root Animator:", animatorObject, typeof(Animator), true);
using (new GUILayout.HorizontalScope())
{
if (animationClips.Length == 1)
animationClips[0] = (AnimationClip) EditorGUILayout.ObjectField("Target Clip:", animationClips[0], typeof(AnimationClip), false);
else
{
EditorGUIUtility.labelWidth = 95;
EditorGUILayout.LabelField("Target Clips:", GUILayout.ExpandWidth(false));
EditorGUIUtility.labelWidth = 1;
EditorGUILayout.LabelField($"Mutiple Anim Clips ({animationClips.Length})");
EditorGUIUtility.labelWidth = 0;
}
}
}
GUILayout.Space(12);
using (new GUILayout.VerticalScope("helpbox"))
{
using (new EditorGUILayout.HorizontalScope("box"))
{
oldPathValue = EditorGUILayout.TextField(oldPathValue);
newPathValue = EditorGUILayout.TextField(newPathValue);
using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(oldPathValue)))
if (GUILayout.Button(new GUIContent("Replace Path","Replaces the old string (left) with the new string (right). Field on the left will be used as a Regex Pattern if Use Regex (@) is enabled.")))
UpdatePath(oldPathValue, newPathValue, true);
regexReplace = GUILayout.Toggle(regexReplace, new GUIContent("@", "Use Regex"), "button", GUILayout.Width(22));
}
GUILayout.Space(12);
DisplayPathItems();
}
GUILayout.Space(40);
GUILayout.EndScrollView();
}
void DisplayPathItems()
{
GUIStyle resetButtonStyle = new GUIStyle() {contentOffset = new Vector2(0, 3.5f)};
for (int i = 0; i < pathsKeys.Count; i++)
{
string path = pathsKeys[i];
GameObject obj = FindObjectInRoot(path);
List<EditorCurveBinding> properties = paths[path];
using (new GUILayout.HorizontalScope())
{
bool isModifiedPath = tempPathOverrides[i] != path;
using (new EditorGUI.DisabledScope(!isModifiedPath))
if (GUILayout.Button(resetIcon, resetButtonStyle, GUILayout.ExpandWidth(false)))
tempPathOverrides[i] = path;
tempPathOverrides[i] = EditorGUILayout.TextField(tempPathOverrides[i]);
using (new EditorGUI.DisabledScope(!isModifiedPath))
if (GUILayout.Button("Apply", GUILayout.Width(60)))
UpdatePath(path, tempPathOverrides[i], false);
GUILayout.Label($"({properties.Count})", GUILayout.Width(40));
Color standardColor = GUI.color;
GUI.color = obj != null ? Color.green : Color.red;
EditorGUI.BeginChangeCheck();
GameObject newObj = (GameObject) EditorGUILayout.ObjectField(obj, typeof(GameObject), true);
if (EditorGUI.EndChangeCheck()) UpdatePath(path, ChildPath(newObj), false);
GUI.color = standardColor;
}
}
}
/*void OnInspectorUpdate() {
this.Repaint();
}*/
private void FillModel()
{
paths.Clear();
pathsKeys.Clear();
foreach ( AnimationClip animationClip in animationClips )
{
FillModelWithCurves(AnimationUtility.GetCurveBindings(animationClip));
FillModelWithCurves(AnimationUtility.GetObjectReferenceCurveBindings(animationClip));
}
tempPathOverrides = pathsKeys.ToArray();
}
private void FillModelWithCurves(EditorCurveBinding[] curves)
{
foreach (EditorCurveBinding curveData in curves)
{
string key = curveData.path;
if (paths.ContainsKey(key))
{
paths[key].Add(curveData);
}
else
{
paths.Add(key, new List<EditorCurveBinding>() { curveData});
pathsKeys.Add(key);
}
}
}
void UpdatePath(string oldPath, string newPath, bool matchWholeWord)
{
if (oldPath == newPath) return;
bool identicalMayExist = !matchWholeWord && paths.TryGetValue(newPath, out _);
try
{
AssetDatabase.StartAssetEditing();
for (int clipIndex = 0; clipIndex < animationClips.Length; clipIndex++)
{
AnimationClip animationClip = animationClips[clipIndex];
Undo.RecordObject(animationClip, "Animation Hierarchy Change");
List<EditorCurveBinding> curves = AnimationUtility.GetCurveBindings(animationClip)
.Concat(AnimationUtility.GetObjectReferenceCurveBindings(animationClip)).ToList();
foreach (var c in curves)
{
EditorCurveBinding binding = c;
AnimationCurve curve = AnimationUtility.GetEditorCurve(animationClip, binding);
ObjectReferenceKeyframe[] objectReferenceCurve = AnimationUtility.GetObjectReferenceCurve(animationClip, binding);
bool isFloatCurve = curve != null;
if (isFloatCurve) AnimationUtility.SetEditorCurve(animationClip, binding, null);
else AnimationUtility.SetObjectReferenceCurve(animationClip, binding, null);
if (!matchWholeWord && binding.path == oldPath)
{
if (identicalMayExist && curves.Any(c2 => c2.path == newPath && c2.type == c.type && c2.propertyName == c.propertyName))
Debug.LogWarning($"Identical settings curve already exists. Skipping curve.\nPath: {c.path}\nType: {c.type.Name}\nProperty: {c.propertyName}");
else binding.path = newPath;
}
else if (matchWholeWord)
{
binding.path = regexReplace
? Regex.Replace(binding.path, oldPath, newPath)
: binding.path.Replace(oldPath, newPath);
}
if (isFloatCurve) AnimationUtility.SetEditorCurve(animationClip, binding, curve);
else AnimationUtility.SetObjectReferenceCurve(animationClip, binding, objectReferenceCurve);
}
DisplayProgress(clipIndex);
}
}
finally
{
AssetDatabase.StopAssetEditing();
EditorUtility.ClearProgressBar();
}
FillModel();
Repaint();
}
#region Automated Methods
private void OnFocus()
{
OnSelectionChange();
}
private void OnEnable()
{
Undo.undoRedoPerformed -= FillModel;
Undo.undoRedoPerformed += FillModel;
resetIcon = new GUIContent(EditorGUIUtility.IconContent("d_Refresh")) { tooltip = "Reset Dimensions" };
}
private void OnDisable()
{
Undo.undoRedoPerformed -= FillModel;
}
#endregion
#region Helper Methods
private GameObject FindObjectInRoot(string path) => animatorObject ? animatorObject.transform.Find(path)?.gameObject : null;
private string ChildPath(GameObject obj)
{
if (animatorObject == null) throw new UnityException("Please assign the Root Animator first!");
if (!obj.transform.IsChildOf(animatorObject.transform)) throw new UnityException($"Object must belong to {animatorObject.name} !");
return AnimationUtility.CalculateTransformPath(obj.transform, animatorObject.transform);
}
private static void DrawTitle(string title)
{
using (new GUILayout.HorizontalScope("in bigtitle"))
GUILayout.Label(title, new GUIStyle("boldLabel"){alignment = TextAnchor.MiddleCenter});
}
private void DisplayProgress(int clipIndex)
{
float fChunk = 1f / animationClips.Length;
float fProgress = fChunk * clipIndex;
EditorUtility.DisplayProgressBar("Animation Hierarchy Progress", "Editing animations.", fProgress);
}
#endregion
}
#endif