summaryrefslogtreecommitdiff
path: root/Assets/Tools/ActionTool/ActionToolGizmos.cs
blob: 938d2c51c3825a329a2d323940e9e48e6a048395 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace ActionTool
{

    public class ActionToolGizmos : MonoBehaviour
    {
        AnimationData m_AnimationData;

        AnimationClip m_Clip;

        AnimationClip clip
        {
            get
            {
                if (m_AnimationData == null)
                    return null;
                if (m_Clip != null && m_Clip.name == m_AnimationData.animationName)
                    return m_Clip;
                m_Clip = AssetDatabase.LoadAssetAtPath< AnimationClip>(m_AnimationData.animationPath);
                return m_Clip;
            }
        }

        float m_CurAnimFrame;

        bool m_IsShowRootMotion;

        GameObject m_UnitRoot;

        public void Initialize(GameObject unitRoot)
        {
            m_UnitRoot = unitRoot;
        }

        public void SetAnimationData(AnimationData data)
        {
            m_AnimationData = data;
        }

        public void ShowRootMotionGizmos(bool show)
        {
            m_IsShowRootMotion = show;
        }

        public void SetCurAnimFrame(float frame)
        {
            m_CurAnimFrame = frame;
        }

        void OnDrawGizmos()
        {
            DrawRoot();
            DrawAxis();
            DrawColliders();

            DrawRootMotion();
        }

        void DrawRoot()
        {
            Gizmos.color = Color.yellow;
            Gizmos.DrawCube(m_UnitRoot.transform.position, new Vector3(0.1f, 0.1f, 0.1f));
        }

        void DrawAxis()
        {
            Gizmos.color = Color.red;
            Gizmos.DrawLine(-Vector3.right * 1000, Vector3.right * 1000);
            Gizmos.color = Color.green;
            Gizmos.DrawLine(Vector3.zero, Vector3.up * 1000);
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(Vector3.zero, Vector3.forward * 1000);
        }

        void DrawColliders()
        {
            if (m_AnimationData == null)
                return;
            DrawBoxes(m_AnimationData.hurtBoxes, Color.green);
            DrawBoxes(m_AnimationData.hitBoxes, Color.red);
        }

        void DrawBoxes(List<ColliderData> boxes, Color color)
        {
            if (boxes != null && boxes.Count > 0)
            {
                for (int i = 0; i < boxes.Count; ++i)
                {
                    var box = boxes[i];
                    if (box != null)
                    {
                        var info = box.GetColliderInfo(m_CurAnimFrame);
                        if (!info.active)
                            continue;
                        Vector3 pos = info.position;
                        switch (box.pivot)
                        {
                            case ColliderBox.Pivot.MiddleBottom:
                                pos.y += info.size.y / 2;
                                break;
                        }
                        pos += m_UnitRoot.transform.position;
                        Gizmos.color = color * 0.5f;
                        Gizmos.DrawCube(pos, info.size);
                    }
                }
            }
        }

        void DrawRootMotion()
        {
            if (m_AnimationData == null)
                return;
            if (!m_AnimationData.overrideRootMotion)
                return;
            if (!m_IsShowRootMotion)
                return;
            if (clip == null)
                return;
            var rm = m_AnimationData.rootMotionOverrideData;
            float frames = clip.length * AnimationData.FPS;
            float step = 0.05f;
            Vector3 prev = rm.GetPosition(0);
            Vector3 cur = prev;
            Gizmos.color = Color.white;
            for (float f = step; f <= frames + step; f+= step)
            {
                cur = rm.GetPosition(f);
                Gizmos.DrawLine(prev, cur);
                prev = cur;
            }
        }

    }
}