summaryrefslogtreecommitdiff
path: root/Assets/ActionTool/Editor/ActionData.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-07-07 18:47:37 +0800
committerchai <chaifix@163.com>2021-07-07 18:47:37 +0800
commita13f10139d33264fc9ebc5a15c75faf16fc7757e (patch)
tree9d6c40a21fc873c6e25ff4bbdeba663a73927427 /Assets/ActionTool/Editor/ActionData.cs
parent1bb4971cffac3851a119f16e815bfe42abfc2df6 (diff)
+Action Tool
Diffstat (limited to 'Assets/ActionTool/Editor/ActionData.cs')
-rw-r--r--Assets/ActionTool/Editor/ActionData.cs225
1 files changed, 225 insertions, 0 deletions
diff --git a/Assets/ActionTool/Editor/ActionData.cs b/Assets/ActionTool/Editor/ActionData.cs
new file mode 100644
index 00000000..33bb5c71
--- /dev/null
+++ b/Assets/ActionTool/Editor/ActionData.cs
@@ -0,0 +1,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;
+ }
+
+ }
+
+}