summaryrefslogtreecommitdiff
path: root/Assets/ActionTool/Editor/ActionColliderEditor.cs
blob: 9bb77515d9fbd36bd6eb52a4ae7242a52e67cf59 (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;

namespace ActionTool
{

    // 编辑事件
    public class ActionColliderEditor : EditorWindow
    {
        EditorWindow sceneView;
        ActionEditorStyles styles;

        ColliderBox collider;

        private void OnEnable()
        {
            titleContent = new GUIContent("Collider Editor");
        }

        private void OnDisable()
        {
            ActionManager.colliderData = null;
            ActionManager.ColliderWindow = null;
            if(ActionManager.PreviewWindow != null)
                ActionManager.PreviewWindow.Repaint();
        }

        private void Update()
        {
        }

        Vector2 scroll;
        private void OnGUI()
        {
            if (ActionManager.colliderData == null)
            {
                this.Close();
                return;
            }

            collider = ActionManager.colliderData.collider;
            if (collider == null)
                return;

            if (styles == null) styles = ActionEditorStyles.Get();

            GUILayout.Space(2);

            EditorGUILayout.LabelField(collider.type.ToString() + " " + ActionManager.colliderIndex, styles.textBoldBig, GUILayout.Height(25));

            GUILayout.Space(2);

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Save"))
            {

            }
            if (GUILayout.Button("Revert"))
            {

            }
            Color prevColor = GUI.backgroundColor;
            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Delete"))
            {
                ActionManager.DeleteCurBox();
            }
            GUI.backgroundColor = prevColor;
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(5);

            scroll = EditorGUILayout.BeginScrollView(scroll);

            Type type = collider.GetType();
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            if (fields != null && fields.Length > 0)
            {
                for (int i = 0; i < fields.Length; ++i)
                {
                    FieldInfo field = fields[i];
                    string name = field.Name + " (" + field.FieldType.Name + ")";
                    string tooltip = "";
                    bool skip = false;
                    foreach (var attr in field.GetCustomAttributes())
                    {
                        if (attr.GetType() == typeof(TooltipAttribute))
                        {
                            TooltipAttribute tooltipattr = attr as TooltipAttribute;
                            if (tooltip != null)
                            {
                                tooltip = tooltipattr.tooltip;
                            }
                        }
                        else if(attr.GetType() == typeof(DisallowModifiyInGUI))
                        {
                            GUI.enabled = false;
                        }
                        else if(attr.GetType() == typeof(ColliderTypeAttribute))
                        {
                            ColliderTypeAttribute t = attr as ColliderTypeAttribute;
                            if (t.type != collider.type)
                            {
                                skip = true;
                                break;
                            }
                        }
                    }
                    if(skip)
                    {
                        GUI.enabled = true;
                        continue;
                    }
                    EditorGUILayout.LabelField(new GUIContent(name, tooltip), styles.textBold);
                    if (field.FieldType == typeof(Vector3))
                    {
                        field.SetValue(collider, GUI_Vector3((Vector3)field.GetValue(collider)));
                    }
                    else if (field.FieldType == typeof(Vector2))
                    {
                        field.SetValue(collider, GUI_Vector2((Vector2)field.GetValue(collider)));
                    }
                    else if (field.FieldType == typeof(string))
                    {
                        field.SetValue(collider, GUI_String((string)field.GetValue(collider)));
                    }
                    else if (field.FieldType == typeof(bool))
                    {
                        field.SetValue(collider, GUI_Bool((bool)field.GetValue(collider)));
                    }
                    else
                    {
                        GUI_Enum(field.GetValue(collider).ToString());
                    }
                    GUI.enabled = true;
                    GUILayout.Space(5);
                }
            }
            EditorGUILayout.EndScrollView();
        }

        Vector3 GUI_Vector3(Vector3 value)
        {
            Rect rect = EditorGUILayout.GetControlRect();
            return EditorGUI.Vector3Field(rect, "", value);
        }

        Vector2 GUI_Vector2(Vector2 value)
        {
            Rect rect = EditorGUILayout.GetControlRect();
            return EditorGUI.Vector2Field(rect, "", value);
        }

        string GUI_String(string value)
        {
            Rect rect = EditorGUILayout.GetControlRect();
            return EditorGUI.TextField(rect, "", value);
        }

        bool GUI_Bool(bool value)
        {
            Rect rect = EditorGUILayout.GetControlRect();
            return GUI.Toggle(rect, value, "Check");
        }

        void GUI_Enum(string value)
        {
            Rect rect = EditorGUILayout.GetControlRect();
            GUI.Label(rect, value);
        }

    }
}