-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptableAnimator.cs
286 lines (246 loc) · 8.51 KB
/
ScriptableAnimator.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
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;
using System.IO;
using UnityEditor.Animations;
using System;
using System.Collections.Generic;
[CustomEditor(typeof(Animator))]
public class AnimationEditor : Editor
{
public override void OnInspectorGUI()
{
Animator animator = (Animator)target;
DrawDefaultInspector();
if (GUILayout.Button("Generate Scipt"))
{
generateBaseClasses();
generateAnimatorScript(animator);
AssetDatabase.Refresh();
}
}
private void generateBaseClasses()
{
generateStateClass();
}
private void generateStateClass()
{
string StateClass = @"using UnityEngine;
namespace IG.Generated.Internal
{
public abstract class State
{
public int Layer { get; protected set; }
public string Name { get; protected set; }
protected Animator animator;
public State(Animator animator)
{
this.animator = animator;
}
public void Play(float offset = 0f)
{
animator.Play(Name, Layer, offset);
}
}
}";
generateFile("State.cs", StateClass);
}
private void generateAnimatorScript(Animator animator)
{
// remove whitespace and minus
var name = secureName(animator.name) + "Animator";
var target = animator.name;
var parameterClass = name + "Parameter";
var content = $@"
using UnityEngine;
using System.Collections;
using IG.Generated.Internal;
namespace IG.Generated.gAnimator {{
// Autogenerated File
public class {name} : MonoBehaviour
{{
private Animator animator;
private static {name} instance;
public {parameterClass} Parameter {{get; private set;}}
{generateLayerList(animator)}
public void Awake() {{
animator = GetComponent<Animator>();
Parameter = new {parameterClass}(animator);
{generateInitLayers(animator)}
}}
}}
public class {parameterClass} {{
private Animator animator;
internal PlayerAnimatorParameter(Animator animator)
{{
this.animator = animator;
}}
{generateParameter(animator)}
}}
{generateLayerClasses(animator)}
}}
";
generateFile(name + ".cs", content);
}
private string generateParameter(Animator animator)
{
var text = new StringBuilder();
var hashes = new Dictionary<string, AnimatorControllerParameter>();
var knownHashes = new List<string>();
foreach (var parameter in animator.parameters)
{
var current = parameter.nameHash.ToString();
if (hashes.ContainsKey(current))
{
var previous = hashes[current];
hashes.Remove(current);
hashes.Add(name, previous);
knownHashes.Add(current);
hashes.Add(parameter.name, parameter);
}
else if (knownHashes.Contains(current))
{
hashes.Add(parameter.name, parameter);
}
else
{
hashes.Add(current, parameter);
}
}
foreach (var hash in hashes)
{
var parameter = hash.Value;
var isHash = hash.Key != parameter.name;
if (parameter.type == AnimatorControllerParameterType.Trigger)
{
text.AppendLine(" public void set" + parameter.name + "() {");
text.AppendLine(" animator.SetTrigger(" + (isHash ? "" : "\"") + hash.Key + (isHash ? "" : "\"") + ");");
text.AppendLine(" }");
}
else
{
var parameterType = parameter.type.ToString().ToLower();
text.AppendLine(" public " + parameterType + " " + parameter.name + "{");
text.AppendLine(" get {");
text.AppendLine(" return animator.Get" + parameter.type.ToString() + "(" + (isHash ? "" : "\"") + hash.Key + (isHash ? "" : "\"") + ");");
text.AppendLine(" }");
text.AppendLine(" set {");
text.AppendLine(" animator.Set" + parameter.type.ToString() + "("+ (isHash ? "" : "\"") + hash.Key + (isHash ? "" : "\"")+", value);");
text.AppendLine(" }");
text.AppendLine(" }");
}
text.AppendLine();
}
return text.ToString();
}
private string generateLayerList(Animator animator)
{
var text = new StringBuilder();
for (var i = 0; i < animator.layerCount; i++)
{
var name = secureName(animator.GetLayerName(i));
text.AppendLine($"public {name}Layer {name} {{ get; private set;}}");
}
return text.ToString();
}
private string generateInitLayers(Animator animator)
{
var text = new StringBuilder();
for (var i = 0; i < animator.layerCount; i++)
{
var name = secureName(animator.GetLayerName(i));
text.AppendLine($"{name} = new {name}Layer(animator);");
}
return text.ToString();
}
private string generateLayerClasses(Animator animator)
{
var text = new StringBuilder();
var controller = animator.runtimeAnimatorController as AnimatorController;
for (var i = 0; i < animator.layerCount; i++)
{
{
text.AppendLine(generateStateMachineClasses(controller.layers[i].stateMachine, i, true));
}
}
return text.ToString();
}
private string generateStateMachineClasses(AnimatorStateMachine stateMachine, int layer, bool isLayer)
{
var text = new StringBuilder();
var subclasses = new StringBuilder();
var name = secureName(stateMachine.name);
var suffix = isLayer ? "Layer" : "StateMachine";
text.AppendLine($"public class {name}{suffix} : State {{");
foreach (var state in stateMachine.states)
{
var stateName = secureName(state.state.name);
text.AppendLine($" public {stateName}State {stateName} {{ get; }}");
}
foreach (var subMachine in stateMachine.stateMachines)
{
var stateName = subMachine.stateMachine.name;
text.AppendLine($" public {stateName}StateMachine {stateName} {{ get; }}");
}
text.AppendLine($"internal {name}{suffix}(Animator animator) : base(animator) {{");
text.AppendLine($" this.Layer={layer};");
text.AppendLine($" this.Name=\"{stateMachine.name}\";");
foreach (var state in stateMachine.states)
{
var stateName = secureName(state.state.name);
text.AppendLine($" {stateName} = new {stateName}State(animator);");
subclasses.AppendLine(generateStateList(state.state, layer));
}
foreach (var subMachine in stateMachine.stateMachines)
{
var stateName = subMachine.stateMachine.name;
text.AppendLine($" {stateName} = new {stateName}StateMachine(animator);");
subclasses.AppendLine(generateStateMachineClasses(subMachine.stateMachine, layer, false));
}
text.AppendLine();
text.AppendLine("}");
text.AppendLine();
text.AppendLine($@" new public void Play(float offset = 0f)
{{
{secureName(stateMachine.defaultState.name)}.Play(offset);
}}");
text.AppendLine();
text.AppendLine("}");
text.AppendLine(subclasses.ToString());
return text.ToString();
}
private string generateStateList(AnimatorState state, int layer)
{
return $@"public class {secureName(state.name)}State : State {{
internal {secureName(state.name)}State(Animator animator) : base(animator) {{
this.Layer={layer};
this.Name=""{state.name}"";
}}
}}
";
}
private void generateFile(string filename, string content)
{
string filePath = "Assets/Generated/Animator/";
var fullPath = filePath + filename;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
FileInfo fi = new FileInfo(fullPath);
if (fi.Exists)
{
fi.Delete();
}
using (StreamWriter outfile =
new StreamWriter(fullPath))
{
outfile.Write(content);
}
}
private string secureName(string name)
{
return name.Replace(" ", "").Replace("-", "_");
}
}