summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/RootMotion/Editor/RootMotionEditor.cs
blob: 734e0ce02c63dbf04f0730c030df0f4ed0beb937 (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
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class RootMotionEditor : EditorWindow
{
    static string s_AnimFolder = "Assets/Art/Animations/";
    static string s_Controller = "Assets/Scripts/Unit/RootMotion/controller_rootmotion.controller";
    static string s_RootMotionDataFolder = "Assets/Data/RootMotionData/";

    GameObject m_Prefab;
    string m_PrefabName { get { return m_Prefab.name; } }
    GameObject m_Unit;
    Transform m_Transform { get { return m_Unit.transform; } }
    string m_SearchText = "";
    HashSet<string> m_SelectAnimations;
    List<string> m_AnimList = new List<string>();

    Animator m_Animator;
    AnimatorOverrideController m_OverrideController;

    const string kEmptyClipName = "Empty";
    const string kStateName = "Action";

    [MenuItem("Custom/RootMotion/Create")]
    static void OpenTool()
    {
        RootMotionEditor editor = GetWindow<RootMotionEditor>();
    }

    private void OnEnable()
    {
        titleContent = new GUIContent("RootMotion Editor");
        m_SelectAnimations = new HashSet<string>();
    }

    private void OnDisable()
    {
        
    }

    private void OnGUI()
    {
        if (m_SelectAnimations == null) m_SelectAnimations = new HashSet<string>();
        if(m_AnimList == null)
        {
            m_AnimList = new List<string>();
            if (m_Prefab != null)
                CollectAnimations(m_Prefab.name);
        }

        EditorGUILayout.LabelField("Select Unit:");
        GameObject prefab = EditorGUILayout.ObjectField(m_Prefab, typeof(GameObject), false) as GameObject;
        if(prefab != m_Prefab)
        {
            OnSelectPrefab(prefab);
        }
        if(m_Prefab == null)
        {
            EditorGUILayout.HelpBox("选择角色", MessageType.Warning);
        }
        GUI_SelectAnimation();
        GUI_Export();
    }

    private void OnSelectPrefab(GameObject prefab)
    {
        m_Prefab = prefab;
        if(m_Prefab != null)
        {
            m_Unit = PrefabUtility.InstantiatePrefab(m_Prefab) as GameObject;
            InitializeUnit(m_Unit);
            m_SelectAnimations.Clear();
            CollectAnimations(m_Prefab.name);
        }
        else
        {
            m_SelectAnimations.Clear();
            m_Unit = null;
        }
    }

    void InitializeUnit(GameObject unit)
    {
        if (unit == null)
            return;

        m_Animator = unit.GetComponent<Animator>();
        m_Animator.applyRootMotion = true;

        RuntimeAnimatorController controller = AssetDatabase.LoadAssetAtPath(s_Controller, typeof(RuntimeAnimatorController)) as RuntimeAnimatorController;
        if (controller)
        {
            m_OverrideController = new AnimatorOverrideController(controller);
            m_Animator.runtimeAnimatorController = m_OverrideController;
        }
    }

    void CollectAnimations(string prefabName)
    {
        m_AnimList.Clear();
        string animFolder = s_AnimFolder + m_Prefab.name + "/";
        string[] animfiles = Directory.GetFiles(animFolder/*, "*.anim"*/);
        if (animfiles != null && animfiles.Length > 0)
        {
            for (int i = 0; i < animfiles.Length; ++i)
            {
                string file = animfiles[i];
                if (file.Contains(".meta"))
                    continue;
                string animName = Path.GetFileNameWithoutExtension(file);
                m_AnimList.Add(animName);
            }
        }
    }

    bool IsAllSelected()
    {
        if (m_SelectAnimations == null || m_SelectAnimations.Count == 0)
            return false;
        if (m_AnimList == null || m_AnimList.Count == 0)
            return false;
        return m_AnimList.Count == m_SelectAnimations.Count;
    }

    Vector2 m_AnimtionListScroll;
    private void GUI_SelectAnimation()
    {
        if (m_Prefab == null)
            return;
        EditorGUILayout.LabelField("Select Animations:");
        m_SearchText = GUILayout.TextField(m_SearchText, "SearchTextField", GUILayout.Width(position.width - 20)).ToLower();

        bool isAllSelected = IsAllSelected();
        bool allselcted = GUILayout.Toggle(isAllSelected, "全选");
        if(allselcted && !isAllSelected)
        {
            m_SelectAnimations.Clear();
            m_AnimList.ForEach(s =>m_SelectAnimations.Add(s));
        }
        else if(!allselcted && isAllSelected)
        {
            m_SelectAnimations.Clear();
        }

        if (m_AnimList != null && m_AnimList.Count > 0)
        {
            m_AnimtionListScroll = EditorGUILayout.BeginScrollView(m_AnimtionListScroll, GUILayout.Height(400));

            for (int i = 0; i < m_AnimList.Count; ++i)
            {
                string file = m_AnimList[i];
                if (file.Contains(".meta"))
                    continue;
                string animName = Path.GetFileNameWithoutExtension(file);
                bool show = m_SearchText == string.Empty || m_SearchText == "" || animName.ToLower().Contains(m_SearchText);
                if (!show)
                    continue;
                bool bChecked = m_SelectAnimations.Contains(animName);
                bool check = GUILayout.Toggle(bChecked, animName, GUILayout.Height(15));
                if (check && !bChecked)
                    m_SelectAnimations.Add(animName);
                else if(!check && bChecked)
                    m_SelectAnimations.Remove(animName);
            }

            EditorGUILayout.EndScrollView();
        }
    }

    void GUI_Export()
    {
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        GUI.enabled = m_SelectAnimations != null && m_SelectAnimations.Count > 0;
        if (GUILayout.Button("Export"))
        {
            ExportRootMotions();
        }
        GUI.enabled = true;
    }

    void ExportRootMotions()
    {
        if (m_SelectAnimations == null || m_SelectAnimations.Count == 0)
            return;

        foreach(var animation in m_SelectAnimations)
        {
            ExportRootMotion(animation);
        }

        EditorUtility.DisplayDialog("Export RootMotion", "目录 " + s_RootMotionDataFolder + m_Prefab.name, "OK");
    }

    void ExportRootMotion(string animation)
    {
        string animPath = s_AnimFolder + m_PrefabName + "/" + animation + ".anim";
        string assetPath = s_RootMotionDataFolder + m_PrefabName + "/" + animation + ".asset";
        AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(animPath);
        if(clip == null)
        {
            Debug.LogError("[RootMotion Editor] 没有对应的animation, " + animPath);
            return;
        }
        m_OverrideController[kEmptyClipName] = clip;

        m_Transform.position = Vector3.zero;
        m_Transform.rotation = Quaternion.identity;

        float frameRate = clip.frameRate;
        float timeLen = clip.length;

        float sampleRate = 30; // rootmotion 采样频率,每秒采样30帧
        float sampleDuration = 1 / sampleRate;

        RootMotionData rootmotion = new RootMotionData();
        rootmotion.animationName = animation;
        rootmotion.animationLength = timeLen;
        rootmotion.fps = sampleRate;

        rootmotion.positionList = new List<Vector3>();
        rootmotion.positionList.Add(Vector3.zero);

        const float kDeltaTime = 1 / 100f;
        float t = 0;
        float sampleTime = 0;

        while(true)
        {
            m_Animator.speed = 1; 
            m_Animator.Play(kStateName, 0, t / timeLen);
            m_Animator.Update(kDeltaTime);
            m_Animator.speed = 0;

            //m_Transform.rotation *= m_Animator.deltaRotation;
            //m_Transform.position += m_Animator.deltaPosition;

            sampleTime += kDeltaTime;
            if(sampleTime >= sampleDuration)
            {
                Vector3 pos = m_Transform.position;
                pos.x = 0;
                pos.y = pos.y < 0 ? 0 : pos.y;

                rootmotion.positionList.Add(pos);
                sampleTime = sampleTime - sampleDuration; 
            }

            if (t == timeLen)
                break;
            t += kDeltaTime;
            if (t > timeLen)
                t = timeLen;
        }

        rootmotion.frameCount = rootmotion.positionList.Count;

        AssetDatabase.CreateAsset((RootMotionData)rootmotion, assetPath);
    }
     
}