summaryrefslogtreecommitdiff
path: root/Assets/ActionTool/Editor/ActionData.cs
blob: 33bb5c71210b002a675d05c1e4d5f07d49d95acb (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
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 float m_TotalFrame;
        private int m_PrevFrame;

        private float m_AnimRate; // 每帧时间

        private float m_CurAnimFrame;
        private double m_PrevLocalTime;

        private const string kStateName = "Action";

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

        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()
        {
            if (!ActionManager.IsPlay)
                return;

            m_CurAnimFrame += (float)(EditorApplication.timeSinceStartup - m_PrevLocalTime) * (ActionManager.FPS * ActionManager.Speed);

            if (m_CurAnimFrame > m_TotalFrame)
            { 
                m_Animator.transform.position = ActionManager.s_InitPosition;
                m_Animator.transform.rotation = ActionManager.s_InitRotation;
            }
            m_CurAnimFrame %= m_TotalFrame;

            SampleFrame(EditorApplication.timeSinceStartup - m_PrevLocalTime);

            RunEvent();

            m_PrevLocalTime = EditorApplication.timeSinceStartup;
        }

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

            m_Animator.speed = 1;

            float normalizeTime = m_CurAnimFrame / m_TotalFrame;

            m_Animator.Play(kStateName, 0, normalizeTime);
            m_Animator.Update((float)dt);

            m_Animator.transform.position += m_Animator.deltaPosition;
            m_Animator.transform.rotation *= m_Animator.deltaRotation;

            m_Animator.speed = 0;
        }

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

        public void RunEvent()
        { 
        }

        public void CreateEvent(TimelineEvent.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.s_AnimationDataFolder + "创建");
                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_AnimRate = 0;
        }

    }

}