-
Notifications
You must be signed in to change notification settings - Fork 0
/
PosterAnimatorGenerate.cs
381 lines (352 loc) · 15.1 KB
/
PosterAnimatorGenerate.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
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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
using System.IO;
using LitJson;
public class PosterAnimationData
{
public int ID;
public int AvatarID;
public string Name;
public int AnimationID;
public int Set;
public string AnimationClip;
public string Parameter;
public bool NeedEndEvent;
public bool IsDefaultIdle;
public int[] ToStateArray;
}
public class PosterAnimatorGenerate : EditorWindow
{
private const string AnimatorSavePath = "Assets/Develop/Art/Roles/Character/";
private const string AnimatorConfigPath = "Assets/Develop/Lua/Config/Table_SecretaryAnimation.lua";
private const string AnimatorClipPrefix = "Assets/Develop/Art/Roles/Character/";
private const string AnimatorClipInfix = "/Dynamic/Poster/Animation/";
private const string AnimatorControllerSuffix = "_PosterController.controller";
private const string PosterPrefabPath = "Assets/Resources/Prefabs/Roles/Character/";
private static string characterName = "";
private static int cur_set = 0; //当前套装
private static string prefab_path = "";
private static float frameRate = 30f;//动画帧率
private static string[] expression_list = {"Common_Angry", "Common_Smiling", "Common_Suprised", "Common_Sad", "Common_Happy"};
private static string[] mouse_list = {"a", "e", "ih", "o", "ou"};
private static AnimationCurve target_curve = AnimationCurve.Linear(0, 0, 15 / frameRate, 100);
private static AnimationCurve other_curve = AnimationCurve.Linear(0, 0, 15 / frameRate, 0);
private static Dictionary<int, PosterAnimationData> animationDic = new Dictionary<int, PosterAnimationData>();
private static Dictionary<int, List<PosterAnimationData>> posterDic = new Dictionary<int, List<PosterAnimationData>>();
private static List<PosterAnimationData> posterAnimationDatas;
private static AnimatorController productController;
[MenuItem("YGame/看板娘/状态机生成界面")]
public static void AnimatorGenerate()
{
LoadConfig();
PosterAnimatorGenerate window = GetWindow<PosterAnimatorGenerate>(true, "PosterAnimatorGenerate");
window.minSize = new Vector2(600, 500);
}
void OnGUI()
{
GUIContent gUIContent = new GUIContent();
GUILayout.BeginArea(new Rect(2f, 2f, 1000f, 600f));
foreach (var item in posterDic)
{
GUILayout.BeginHorizontal();
gUIContent.text = item.Value[0].Name + item.Value[0].Set;
GUILayout.Label(gUIContent, GUILayout.ExpandWidth(false));
if (GUILayout.Button("ExportAnimator", GUILayout.ExpandWidth(false)))
{
SetCurConfig(item.Value);
SingleAnimatorGenerate();
EditorUtility.DisplayDialog("提示", "导出成功", "确定");
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
}
if (GUILayout.Button("ExportAllAnimator", GUILayout.ExpandWidth(false)))
{
int count = 0;
foreach (var item in posterDic)
{
count++;
SetCurConfig(item.Value);
EditorUtility.DisplayProgressBar("导出进度", characterName + cur_set, count/posterDic.Count);
SingleAnimatorGenerate();
EditorUtility.ClearProgressBar();
}
EditorUtility.DisplayDialog("提示", "导出成功", "确定");
}
GUILayout.EndArea();
}
public static void SetCurConfig(List<PosterAnimationData> _posterAnimationDatas)
{
posterAnimationDatas = _posterAnimationDatas;
characterName = posterAnimationDatas[0].Name;
cur_set = posterAnimationDatas[0].Set;
prefab_path = PosterPrefabPath + characterName + "/P_"+ characterName + cur_set + "_Poster_HQ.prefab";
}
//单个导出
public static void SingleAnimatorGenerate()
{
CreateController();
AddParameters();
FaceAnimationGenerate();
CreateAnimStates();
BindControllerToPrefab();
}
//加载解析看板娘所需配置
static void LoadConfig()
{
animationDic.Clear();
posterDic.Clear();
if (!File.Exists(AnimatorConfigPath))
{
Debug.LogError("找不到数据:" + AnimatorConfigPath);
return;
}
var data = GetTextFromFile(AnimatorConfigPath);
var json = Json2TableTools.GetJsonFromLua(data);
var jsonData = JsonMapper.ToObject<Dictionary<string, PosterAnimationData>>(json);
foreach (var item in jsonData)
{
animationDic.Add(int.Parse(item.Key), item.Value);
int avatar_id = item.Value.AvatarID;
List<PosterAnimationData> poster_list;
if (!posterDic.TryGetValue(avatar_id, out poster_list))
{
poster_list = new List<PosterAnimationData>();
posterDic.Add(avatar_id, poster_list);
}
poster_list.Add(item.Value);
}
}
//创建AnimatorController
static void CreateController()
{
string path = AnimatorSavePath + characterName + "/Set" + cur_set + "/Dynamic/Poster/Animation/" + characterName + AnimatorControllerSuffix;
productController = AnimatorController.CreateAnimatorControllerAtPath(path);
productController.layers = null;
productController.AddLayer("Default");
productController.AddLayer("Face");//表情层
productController.parameters = null;
var layers = productController.layers;
layers[0].defaultWeight = 1;
layers[0].blendingMode = AnimatorLayerBlendingMode.Override;
layers[1].defaultWeight = 1;
layers[1].blendingMode = AnimatorLayerBlendingMode.Additive;
productController.layers = layers;
// var layer = new UnityEditor.Animations.AnimatorControllerLayer
// {
// name = "Face",
// defaultWeight = 1f,
// blendingMode = AnimatorLayerBlendingMode.Additive,
// stateMachine = new UnityEditor.Animations.AnimatorStateMachine() // Make sure to create a StateMachine as well, as a default one is not created
// };
// productController.AddLayer("Face");
// productController.AddLayer(layer);
}
//表情动画生成
static void FaceAnimationGenerate()
{
AnimatorStateMachine stateMachine = productController.layers[1].stateMachine;
AnimatorState idle_state = stateMachine.AddState("Idle", new Vector2(0, 300));
Vector2 position = new Vector2(400, 0);
SkinnedMeshRenderer face_renderer = null;
GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(prefab_path);
SkinnedMeshRenderer[] renderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>();
foreach (var item in renderers)
{
if (item.name.Contains("face") || item.name.Contains("Face"))
{
face_renderer = item;
break;
}
}
Mesh sharedMesh = face_renderer.sharedMesh;
string idle_path = AnimatorClipPrefix + characterName + "/Set" + cur_set
+ AnimatorClipInfix + characterName + cur_set + "_Poster_Common_Idle.anim";
AnimationClip idle_clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(idle_path);
bool is_idle_clip_exist = idle_clip != null;
if (!is_idle_clip_exist)
{
idle_clip = new AnimationClip();
}
for (int j = 0; j < sharedMesh.blendShapeCount; j++)
{
string name = sharedMesh.GetBlendShapeName(j);
string propertyName = "blendShape." + name;
idle_clip.SetCurve(face_renderer.name, typeof(SkinnedMeshRenderer), propertyName, other_curve);
}
if (is_idle_clip_exist)
{
EditorUtility.SetDirty(idle_clip);
}
else
{
AssetDatabase.CreateAsset(idle_clip, idle_path);
}
idle_state.motion = idle_clip;
for (int i = 0; i < expression_list.Length; i++)
{
string clip_Path = AnimatorClipPrefix + characterName + "/Set" + cur_set
+ AnimatorClipInfix + characterName + cur_set + "_Poster_"+ expression_list[i] + ".anim";
AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(clip_Path);
bool is_clip_exist = clip != null;
if (!is_clip_exist)
{
clip = new AnimationClip();
}
string expression = "R_" + characterName + cur_set + "_" + expression_list[i];
for (int j = 0; j < sharedMesh.blendShapeCount; j++)
{
string name = sharedMesh.GetBlendShapeName(j);
string propertyName = "blendShape." + name;
if (name.Equals(expression))
{
clip.SetCurve(face_renderer.name, typeof(SkinnedMeshRenderer), propertyName, target_curve);
}
else if(Array.IndexOf(mouse_list, name) == -1)
{
clip.SetCurve(face_renderer.name, typeof(SkinnedMeshRenderer), propertyName, other_curve);
}
}
if (is_clip_exist)
{
EditorUtility.SetDirty(clip);
}
else
{
AssetDatabase.CreateAsset(clip, clip_Path);
}
position.y = 130*i;
AnimatorState state_1 = stateMachine.AddState(clip.name, position);
state_1.motion = clip;
AnimatorStateTransition transition_1 = idle_state.AddTransition(state_1);
transition_1.AddCondition(AnimatorConditionMode.If, 0, expression_list[i]);
AnimatorStateTransition transition_2 = state_1.AddTransition(idle_state);
transition_2.AddCondition(AnimatorConditionMode.If, 0, expression_list[i] + "_reverse");
position.y = 130*i + 60;
AnimatorState state_2 = stateMachine.AddState(clip.name + "_reverse", position);
state_2.motion = clip;
state_2.speed = -1;
}
EditorUtility.SetDirty(productController);
// AssetDatabase.Refresh();
}
//AnimatorController添加Parameters
static void AddParameters()
{
HashSet<string> hashSet = new HashSet<string>();
for (int i = 0; i < posterAnimationDatas.Count; i++)
{
string trigger = posterAnimationDatas[i].Parameter;
if (!string.IsNullOrEmpty(trigger))
{
if (!hashSet.Contains(trigger))
{
hashSet.Add(trigger);
productController.AddParameter(trigger, AnimatorControllerParameterType.Trigger);
}
}
}
hashSet.Clear();
for (int i = 0; i < expression_list.Length; i++)
{
productController.AddParameter(expression_list[i], AnimatorControllerParameterType.Trigger);
productController.AddParameter(expression_list[i] + "_reverse", AnimatorControllerParameterType.Trigger);
}
}
static void CreateAnimStates()
{
AnimatorStateMachine stateMachine = null;
List<AnimatorState> state_list = new List<AnimatorState>();
for (int i = 0; i < posterAnimationDatas.Count; i++)
{
string clip_Path = AnimatorClipPrefix + characterName + "/Set" + posterAnimationDatas[i].Set
+ AnimatorClipInfix + posterAnimationDatas[i].AnimationClip + ".anim";
AnimationClip clip = AssetDatabase.LoadAssetAtPath(clip_Path, typeof(AnimationClip)) as AnimationClip;
stateMachine = productController.layers[0].stateMachine;
Vector2 position = Vector2.zero;
if (posterAnimationDatas[i].AnimationClip.Contains("Idle"))
{
position = new Vector2(400, 600);
}
else if (posterAnimationDatas[i].AnimationClip.Contains("Touch"))
{
position = new Vector2(800, 80 * i);
}
else if (posterAnimationDatas[i].AnimationClip.Contains("MainInShow"))
{
position = new Vector2(400, 200);
}
else
{
position = new Vector2(0, 80 * i);
}
AnimatorState state = stateMachine.AddState(posterAnimationDatas[i].AnimationClip, position);
state.motion = clip;
if (posterAnimationDatas[i].NeedEndEvent)
{
state.AddStateMachineBehaviour<PosterStateExitBehaviour>();
}
if (posterAnimationDatas[i].IsDefaultIdle)
{
stateMachine.defaultState = state;
}
state_list.Add(state);
}
for (int i = 0; i < state_list.Count; i++)
{
for (int j = 0; j < posterAnimationDatas[i].ToStateArray.Length; j++)
{
int to_index = GetToStateRealIndex(posterAnimationDatas[i].ToStateArray[j]);
AnimatorStateTransition transition = state_list[i].AddTransition(state_list[to_index]);
if (!string.IsNullOrEmpty(posterAnimationDatas[to_index].Parameter))
{
transition.hasExitTime = false;
transition.AddCondition(AnimatorConditionMode.If, 0, posterAnimationDatas[to_index].Parameter);
}
else
{
transition.hasExitTime = true;
transition.exitTime = 0.75f;
}
}
}
EditorUtility.SetDirty(productController);
AssetDatabase.SaveAssets();
}
static int GetToStateRealIndex(int index)
{
for (int i = 0; i < posterAnimationDatas.Count; i++)
{
if (posterAnimationDatas[i].AnimationID == index)
{
return i;
}
}
return 0;
}
static void BindControllerToPrefab()
{
GameObject prefab = AssetDatabase.LoadAssetAtPath(prefab_path, typeof(GameObject)) as GameObject;
GameObject obj = Instantiate(prefab);
GameObject body = obj.transform.GetChild(0).GetChild(0).gameObject;
Animator animator = body.GetComponent<Animator>();
if (animator == null)
{
animator = body.AddComponent<Animator>();
}
animator.runtimeAnimatorController = productController;
PrefabUtility.SaveAsPrefabAsset(obj, prefab_path);
DestroyImmediate(obj);
}
static string GetTextFromFile(string filePath)
{
var sr = new StreamReader(filePath);
var data = sr.ReadToEnd();
sr.Close();
sr.Dispose();
return data;
}
}