blob: 08d48781ca59009198d281718dc2b443ff4b537a (
plain)
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace ActionTool
{
public static class ActionManager
{
public static string s_AnimFolder = "Assets/Art/Animations/";
public static string s_Controller = "Assets/ActionTool/controller_temp.controller";
public static string s_AnimationDataFolder = "Assets/Data/AnimationData/";
static string s_RootMotionDataFolder = "Assets/Data/RootMotionData/";
public static ActionEditor AnimationWindow;
public static ActionPreviewEditor PreviewWindow;
public static ActionEventEditor EventEditWindow;
public static ActionColliderEditor ColliderWindow;
//规定timeline上事件的采样频率是30帧
public const int FPS = 30;
public static float Speed = 1;
public static GameObject CurrentUnit
{
get
{
return s_CurrentUnit;
}
}
public static string CurrentAnimationName
{
get
{
return s_CurrentAnimationName;
}
}
private static GameObject s_CurrentUnit;
private static string s_CurrentAnimationName;
private static GameObject s_UnitInstance;
private static Animator s_Animator;
private static AnimatorOverrideController s_OverrideContorller;
private static ActionData s_CurActionData;
public static ActionData actionData { get { return s_CurActionData; } }
public static Vector3 s_InitPosition = Vector3.zero;
public static Quaternion s_InitRotation = Quaternion.identity;
private static RootMotionData s_RootMotion;
private static ActionToolGizmos s_Gizmos;
public static ActionToolGizmos gizmos { get { return s_Gizmos; } }
// 是否在自动播放
private static bool s_IsPlay;
public static bool IsPlay { get { return s_IsPlay; } }
private static GameObject s_RootActionTool;
private const string kRootActionTool = "RootActionTool";
public const int kMaxEventsPerFrame = 10;
public static void OnSelectObj(GameObject obj)
{
Release();
s_CurrentUnit = obj;
if(s_CurrentUnit != null)
{
s_UnitInstance = GameObject.Instantiate(obj);
InitializeUnitInstance(s_UnitInstance);
}
}
public static void OnSelectAnimation(string animation)
{
s_CurrentAnimationName = animation;
if (PreviewWindow == null)
PreviewWindow = EditorWindow.GetWindow<ActionPreviewEditor>();
PreviewWindow.Repaint();
string animpath = s_AnimFolder + s_CurrentUnit.name + "/" + animation + ".anim";
AnimationClip clip = AssetDatabase.LoadAssetAtPath(animpath, typeof(AnimationClip)) as AnimationClip;
if (clip)
{
s_OverrideContorller["EmptyAction"] = clip;
}
string rootmotionData = s_RootMotionDataFolder + s_CurrentUnit.name + "/" + animation + ".asset";
s_RootMotion = AssetDatabase.LoadAssetAtPath<RootMotionData>(rootmotionData);
if (s_RootMotion == null)
{
Debug.LogError("[ActionTool] 没有对应的rootmotion, " + rootmotionData);
}
if (s_CurActionData == null)
s_CurActionData = new ActionData();
s_CurActionData.Initialize(s_Animator, clip, s_RootMotion);
}
public static bool HasSelectObj()
{
return s_CurrentUnit != null;
}
public static string GetUnitName()
{
if (s_CurrentUnit == null)
return null;
return s_CurrentUnit.name;
}
public static void Release()
{
if(s_UnitInstance != null)
GameObject.DestroyImmediate(s_UnitInstance);
s_UnitInstance = null;
s_CurrentUnit = null;
s_CurrentAnimationName = null;
s_Animator = null;
s_RootActionTool = GameObject.Find(kRootActionTool);
if (s_RootActionTool)
GameObject.DestroyImmediate(s_RootActionTool);
s_CurActionData = null;
}
private static void InitializeUnitInstance(GameObject unit)
{
if (unit == null)
return;
unit.transform.position = s_InitPosition;
unit.transform.rotation = s_InitRotation;
s_RootActionTool = GameObject.Find(kRootActionTool);
if(s_RootActionTool == null)
s_RootActionTool = new GameObject(kRootActionTool);
unit.transform.SetParent(s_RootActionTool.transform);
s_Animator = unit.GetComponentInChildren<Animator>();
if(s_Animator == null)
{
Debug.LogError("[ActionTool] 角色prefab下没有animator");
return;
}
RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath(s_Controller, typeof(RuntimeAnimatorController)) as RuntimeAnimatorController;
if(controller == null)
{
Debug.LogError("[ActionTool] 文件丢失" + s_Controller);
return;
}
s_OverrideContorller = new AnimatorOverrideController(controller);
s_OverrideContorller.name = "override controller";
s_Animator.runtimeAnimatorController = s_OverrideContorller;
s_Animator.applyRootMotion = false;
s_Gizmos = unit.AddComponent<ActionToolGizmos>();
}
public static void UpdateFrame()
{
if (s_CurActionData != null)
s_CurActionData.UpdateFrame();
}
public static void Pause()
{
s_IsPlay = !s_IsPlay;
if(s_IsPlay && s_CurActionData != null)
{
s_CurActionData.StartFrame();
}
}
public static void Start()
{
if (s_CurActionData != null)
s_CurActionData.curAnimTimeNormal = 0;
}
public static void Stop()
{
if (s_CurActionData != null)
{
if (s_IsPlay)
Pause();
s_CurActionData.curAnimTimeNormal = 0;
}
}
public static void Previous()
{
if (s_IsPlay)
Pause();
float cur = s_CurActionData.curAnimFrame;
float pre = Mathf.Ceil(cur - 1);
pre = (int)Mathf.Clamp(pre, 0, s_CurActionData.totalFrame);
s_CurActionData.SetCurrentAnimTime(pre);
}
public static void Next()
{
if (s_IsPlay)
Pause();
float cur = s_CurActionData.curAnimFrame;
float next = Mathf.Floor(cur + 1);
next = (int)Mathf.Clamp(next, 0, s_CurActionData.totalFrame);
s_CurActionData.SetCurrentAnimTime(next);
}
public static void End()
{
if (s_CurActionData != null)
s_CurActionData.curAnimTimeNormal = 1;
}
}
}
|