summaryrefslogtreecommitdiff
path: root/Assets/Tools/ActionTool/Editor/ActionData.cs
blob: 3da313888e3a3f0026722f3d149fc73e5ac3bf12 (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
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
382
383
384
385
386
387
388
389
390
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using UnityEngine;
using UnityEditor;

namespace ActionTool
{
    // 正在编辑的动画
    public class ActionData
    {
        private Animator m_Animator;
        private AnimationClip m_Clip;
        
        private AnimationData m_AnimData; // asset

        private List<AnimationEventBase> m_EventList { get { return m_AnimData != null ? m_AnimData.animationEvents : null; } }

        private AnimationEventBase m_CurEventInfo; // 当前正在编辑的event

        private TimelineEventProxy m_TimelineEventProxy;

        private static List<Projectile> m_Projectiles = new List<Projectile>();
        private static List<FxClear> m_ParticleSystems = new List<FxClear>();

        #region metadata
        private float m_TotalFrame; //timeline采样的总帧数
        public float totalFrame { get { return m_TotalFrame; } }
        private float m_SampleRate; // 每次采样的时间间隔
        public float sampleRate { get { return m_SampleRate; } }
        #endregion

        public float curAnimFrame { get { return m_CurAnimFrame; } }

        public float curAnimTimeNormal
        {
            get
            {
                return m_CurAnimFrame / m_TotalFrame;
            }
            set
            {
                float v = value % m_TotalFrame;
                m_CurAnimFrame = v * m_TotalFrame;
            }
        }

        private int m_PrevFrame;
        private float m_CurAnimFrame;
        private double m_PrevLocalTime;
        private float m_PrevNormalTime;

        private int m_PrevAnimEventFrame; // 上次的帧数(整数)

        private bool m_NotApplyCurves;
		public bool applyCurves { get { return !m_NotApplyCurves; } set { m_NotApplyCurves = !value; } } // 是否开启curve控制速度
		private bool m_NotApplyCurve;
        public bool applyCurve { get { return !m_NotApplyCurve; } set { m_NotApplyCurve = !value; } } // 是否开启curve控制速度
        private bool m_NotApplyRM;
        public bool applyRootMotion { get { return !m_NotApplyRM; } set { m_NotApplyRM = !value; } }

        private const string kStateName = "Action";

        public RootMotionData rootMotion { get{ return m_RootMotion; } }
        private RootMotionData m_RootMotion;

        public string rootMotionPath
        {
            get
            {
                return AssetDatabase.GetAssetPath(m_RootMotion);
            }
        }

        public void Initialize(Animator animator, AnimationClip clip, RootMotionData rootmotion)
        {
            m_Animator = animator;
            m_Clip = clip;
            if(m_Clip != null)
            {
                m_TotalFrame = m_Clip.length * ActionManager.FPS;
                m_SampleRate = m_Clip.length / m_TotalFrame;
            }
            m_PrevFrame = -1;
            m_CurAnimFrame = 0;
            m_PrevLocalTime = 0;
            m_Animator.Play(kStateName, 0, 0);
            m_RootMotion = rootmotion;

			m_TimelineEventProxy = new TimelineEventProxy(ActionManager.unitRoot.transform);
            m_TimelineEventProxy.isInEditMode = true;
            m_TimelineEventProxy.registerProjectile = RegisterProjectile;
            m_TimelineEventProxy.registerParticleSystem = RegisterParticleSystem;
        }

        public void SetCurrentAnimTime(float time)
        {
            m_CurAnimFrame = time;
        }

        public void SetCurrentEvent(int index)
        {
            if(index < 0)
            {
                m_CurEventInfo = null;
            }
            else if(m_EventList != null)
            {
                m_CurEventInfo = m_EventList[index];
            }
            else
            {
                m_CurEventInfo = null;
            }
        }

        public AnimationEventBase GetEvent(int index)
        {
            if(m_EventList != null && index >= 0 && index < m_EventList.Count)
            {
                return m_EventList[index];
            }
            else
            {
                return null;
            }
        }

        public int GetEventCount()
        {
            if (m_EventList != null)
                return m_EventList.Count;
            return 0;
        }

        public void RemoveCurrentEvent()
        {
            if(m_EventList != null)
            {
                m_EventList.Remove(m_CurEventInfo);
                m_CurEventInfo = null;
            }
        }

        public void RemoveAllEvent()
        {
            if(m_EventList != null)
            {
                m_EventList.Clear();
                m_CurEventInfo = null;
            }
        }

        public void StartFrame()
        {
            m_PrevFrame = -1;
            m_PrevLocalTime = EditorApplication.timeSinceStartup;
        }

        public void UpdateFrame()
        {
            float dt = (float)(EditorApplication.timeSinceStartup - m_PrevLocalTime);

            if (ActionManager.IsPlay)
            {
                float deltaFrame = dt * ActionManager.FPS * ActionManager.Speed;

                if (applyCurve)
                {
                    float normalizeTime = m_CurAnimFrame / m_TotalFrame;
                    AnimationData animData = ActionManager.animationData;
                    if (animData)
                    {
                        AnimationCurve curve = animData.speedCurve;
                        deltaFrame *= curve.Evaluate(normalizeTime);
                    }
                }

                m_CurAnimFrame += deltaFrame;

				if (m_CurAnimFrame > m_TotalFrame)
                {
                    ActionManager.ResetUnitRootPosAndRot();
                    m_PrevNormalTime = 0;
                }
                m_CurAnimFrame %= m_TotalFrame;
            }

            UpdateParticles(dt);

            UpdateProjectiles(dt);

            ActionManager.gizmos.SetCurAnimFrame(m_CurAnimFrame);

            SampleFrame();

            RunEvent();

            m_PrevLocalTime = EditorApplication.timeSinceStartup;
        }

        // 播放当前帧
        public void SampleFrame()
        {
            if (m_Animator == null)
                return;

            float normalizeTime = m_CurAnimFrame / m_TotalFrame;

            if(applyRootMotion)
            {
                var animData = ActionManager.animationData;
                bool overrideRM = animData != null && animData.overrideRootMotion == true; 
                
                if(!overrideRM && m_RootMotion)
                {
                    // Action Tool这里需要转换一下root motion的轴
                    ActionManager.unitRoot.transform.position = RootMotionUtility.ExchangeXZ(m_RootMotion.GetRootMotion(normalizeTime));
                }
                else if(overrideRM)
                {
                    if(!ActionRootMotionEditor.IsRecord)
                    {
                        ActionManager.unitRoot.transform.position = animData.rootMotionOverrideData.GetPosition(m_CurAnimFrame);
                    }
                    else // 只在第一次播到这一帧的时候设置位置,否则场景里没法编辑位置
                    {
                        int curAnimEventFrame = (int)m_CurAnimFrame;
                        normalizeTime = curAnimEventFrame / m_TotalFrame;
                        if (curAnimEventFrame != m_PrevAnimEventFrame)
                        {
                            ActionManager.unitRoot.transform.position = animData.rootMotionOverrideData.GetPosition(curAnimEventFrame);
                        }
                        m_PrevAnimEventFrame = curAnimEventFrame;
                    }
                }
            }

            m_Animator.speed = 1;
            m_Animator.Play(kStateName, 0, normalizeTime);
            m_Animator.Update(0);
            m_Animator.speed = 0;

        }

        public int GetCurrentFrame()
        {
            float animTime = sampleRate * m_CurAnimFrame;
            int curFrame = Mathf.RoundToInt(animTime * ActionManager.FPS);
            return curFrame;
        }

        public void RunEvent()
        {
            if (m_TimelineEventProxy != null)
				m_TimelineEventProxy.ExecuteAnimationEvents(ActionManager.animationData, m_CurAnimFrame);
        }

        public void CreateEvent(TimelineEventProxy.EEventType eventtype, int startFrame)
        {
            var classes = Assembly
               .GetAssembly(typeof(AnimationEventBase))
               .GetTypes()
               .Where(t => t.IsSubclassOf(typeof(AnimationEventBase)));
            Type type = null;
            foreach (var itor in classes)
            {
                string name = itor.Name;
                if(itor.Name == eventtype.ToString())
                {
                    type = itor;
                    break;
                }
            }
            if(type != null)
            {
                var e = Activator.CreateInstance(type) as AnimationEventBase;
                //e.type = eventtype;
                e.startFrame = startFrame;
                AddEvent(e);
            }
            else
            {
                Debug.LogError("[ActionTool] 没有对应类型的event" + eventtype.ToString());
            }
        }

        public void AddEvent(AnimationEventBase evnt)
        {
            if (m_AnimData == null || m_EventList == null)
            {
                Debug.LogError("[ActionTool] 没有对应的action数据,是否在" + ActionManager.unitAnimationDataFolder + "创建");
                return;
            }
            m_EventList.Add(evnt);
        }

        public void SaveActionData()
        {
            if (m_AnimData == null)
                return;
            EditorUtility.SetDirty(m_AnimData);
            AssetDatabase.SaveAssets();
        }

        public void Clear()
        {
            m_Animator = null;
            m_Clip = null;
            m_AnimData = null;
            m_CurEventInfo = null;
            m_TotalFrame = 0;
            m_CurAnimFrame = 0;
            m_PrevFrame = 0;
            m_PrevLocalTime = 0;
            m_SampleRate = 0;
        }

        public static void RegisterProjectile(Projectile projectile)
        {
            m_Projectiles.Add(projectile);
        }

        public static void RegisterParticleSystem(FxClear vfx)
        {
            m_ParticleSystems.Add(vfx);
        }

        void UpdateProjectiles(float dt)
        {
            List<Projectile> removed = ListPool<Projectile>.Get();  
            // 更新projectile
            for (int i = 0; i < m_Projectiles.Count; ++i)
            {
                if (m_Projectiles[i] == null)
                    continue;
                m_Projectiles[i].Update(dt);
                if (m_Projectiles[i] != null)
                {
                    TransformEx.DoRecursively(m_Projectiles[i].transform, (t) => {
                        ParticleSystem ps = t.GetComponent<ParticleSystem>();
                        if (ps != null)
                        {
                            ps.Simulate(m_Projectiles[i].time);
                        }
                    }, true);
                }
                else
                {
                    removed.Add(m_Projectiles[i]);
                }
            }
            ListPool<Projectile>.Release(removed);
        }

        void UpdateParticles(float dt)
        {
            List<FxClear> removed = ListPool<FxClear>.Get();
            // 更新粒子系统
            for (int i = 0; i < m_ParticleSystems.Count; ++i)
            {
                if (m_ParticleSystems[i] == null)
                    continue;
                if (m_ParticleSystems[i] != null)
                {
                    m_ParticleSystems[i].UpdateFunc(dt);
                    if (m_ParticleSystems[i] != null)
                    {
                        TransformEx.DoRecursively(m_ParticleSystems[i].transform, (t) => {
                            ParticleSystem ps = t.GetComponent<ParticleSystem>();
                            if (ps != null)
                            {
                                ps.Simulate(m_ParticleSystems[i].time);
                            }
                        }, true);
                    }
                    else
                    {
                        removed.Add(m_ParticleSystems[i]);
                    }
                }
            }
            ListPool<FxClear>.Release(removed);
        }

    }

}