Repository navigation
Expand file tree
/
Copy pathDefinitionEditorWindow.cs
More file actions
189 lines (162 loc) · 7.63 KB
/
Copy pathDefinitionEditorWindow.cs
File metadata and controls
189 lines (162 loc) · 7.63 KB
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
using UnityEditor;
using UnityEngine;
namespace TopView.Editor
{
// Definition 에셋을 탭별로 모아 보여 주고, 고른 에셋의 인스펙터를 오른쪽에 그리는 편집 창
public class DefinitionEditorWindow : EditorWindow
{
private DefinitionEditorTab _currentTab = DefinitionEditorTab.Settings;
// 현재 탭의 묶음별 에셋. 번호가 TAB_GROUPS의 묶음 번호와 같다
private ScriptableObject[][] _groupAssets;
private ScriptableObject _selectedAsset;
private UnityEditor.Editor _assetEditor;
private Vector2 _listScrollPosition;
private Vector2 _detailScrollPosition;
// 번호가 DefinitionEditorTab과 같다
private static readonly string[] TAB_LABELS = { "Settings", "Weapons", "Player", "Enemy", "Loot" };
// 탭마다 목록을 나누는 묶음. 바깥 번호가 DefinitionEditorTab과 같다. 묶음이 하나뿐인 탭은 머리글을 두지 않는다
private static readonly DefinitionEditorGroup[][] TAB_GROUPS =
{
new DefinitionEditorGroup[]
{
new DefinitionEditorGroup { label = "Game Settings", filter = "t:GameSettingsDefinition" },
new DefinitionEditorGroup { label = "Menu Settings", filter = "t:MenuSettingsDefinition" },
new DefinitionEditorGroup { label = "Audio Settings", filter = "t:AudioSettingsDefinition" },
new DefinitionEditorGroup { label = "Music", filter = "t:MusicDefinition" },
new DefinitionEditorGroup { label = "User Interface Sound", filter = "t:UserInterfaceSoundDefinition" }
},
new DefinitionEditorGroup[] { new DefinitionEditorGroup { filter = "t:WeaponDefinition" } },
new DefinitionEditorGroup[] { new DefinitionEditorGroup { filter = "t:PlayerDefinition" } },
new DefinitionEditorGroup[] { new DefinitionEditorGroup { filter = "t:EnemyDefinition" } },
new DefinitionEditorGroup[] { new DefinitionEditorGroup { filter = "t:LootDefinition" } }
};
private const string WINDOW_TITLE = "Definition Editor";
private const string BOX_STYLE = "box";
private const float TAB_HEIGHT = 30f;
private const float LIST_WIDTH = 250f;
private const float BUTTON_HEIGHT = 25f;
// 오른쪽 패널 너비 중 라벨이 차지하는 비율
private const float LABEL_WIDTH_RATIO = 0.45f;
// 라벨 너비의 최솟값. Unity 기본 라벨 너비와 같다
private const float MIN_LABEL_WIDTH = 150f;
/// <summary>
/// 편집 창을 연다.
/// </summary>
[MenuItem("Tools/Definition Editor")]
public static void ShowWindow()
{
GetWindow<DefinitionEditorWindow>(WINDOW_TITLE);
}
private void OnEnable()
{
LoadAssets();
}
private void LoadAssets()
{
DefinitionEditorGroup[] groups = TAB_GROUPS[(int)_currentTab];
_groupAssets = new ScriptableObject[groups.Length][];
for (int groupIndex = 0; groupIndex < groups.Length; groupIndex++)
{
string[] assetIdentifiers = AssetDatabase.FindAssets(groups[groupIndex].filter);
ScriptableObject[] assets = new ScriptableObject[assetIdentifiers.Length];
for (int assetIndex = 0; assetIndex < assetIdentifiers.Length; assetIndex++)
{
string path = AssetDatabase.GUIDToAssetPath(assetIdentifiers[assetIndex]);
assets[assetIndex] = AssetDatabase.LoadAssetAtPath<ScriptableObject>(path);
}
_groupAssets[groupIndex] = assets;
}
}
private void OnGUI()
{
EditorGUI.BeginChangeCheck();
_currentTab = (DefinitionEditorTab)GUILayout.Toolbar((int)_currentTab, TAB_LABELS, GUILayout.Height(TAB_HEIGHT));
if (EditorGUI.EndChangeCheck())
{
_selectedAsset = null;
if (_assetEditor != null)
{
DestroyImmediate(_assetEditor);
}
LoadAssets();
}
EditorGUILayout.Space();
// 왼쪽 목록과 오른쪽 인스펙터로 나눈다
EditorGUILayout.BeginHorizontal();
DrawListPanel();
DrawDetailPanel();
EditorGUILayout.EndHorizontal();
}
private void DrawListPanel()
{
EditorGUILayout.BeginVertical(BOX_STYLE, GUILayout.Width(LIST_WIDTH), GUILayout.ExpandHeight(true));
_listScrollPosition = EditorGUILayout.BeginScrollView(_listScrollPosition);
if (GUILayout.Button("Refresh", GUILayout.Height(BUTTON_HEIGHT)))
{
LoadAssets();
}
EditorGUILayout.Space();
if (_groupAssets != null)
{
DefinitionEditorGroup[] groups = TAB_GROUPS[(int)_currentTab];
for (int groupIndex = 0; groupIndex < _groupAssets.Length; groupIndex++)
{
string label = groups[groupIndex].label;
if (label != null)
{
if (groupIndex > 0)
{
EditorGUILayout.Space();
}
EditorGUILayout.LabelField(label, EditorStyles.boldLabel);
}
DrawAssetButtons(_groupAssets[groupIndex]);
}
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}
private void DrawAssetButtons(ScriptableObject[] assets)
{
foreach (ScriptableObject asset in assets)
{
if (asset == null)
{
continue;
}
GUI.backgroundColor = (_selectedAsset == asset) ? Color.cyan : Color.white;
if (GUILayout.Button(asset.name, GUILayout.Height(BUTTON_HEIGHT)))
{
_selectedAsset = asset;
if (_assetEditor != null)
{
DestroyImmediate(_assetEditor);
}
_assetEditor = UnityEditor.Editor.CreateEditor(_selectedAsset);
}
GUI.backgroundColor = Color.white;
}
}
private void DrawDetailPanel()
{
EditorGUILayout.BeginVertical(BOX_STYLE, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
_detailScrollPosition = EditorGUILayout.BeginScrollView(_detailScrollPosition);
if (_selectedAsset != null && _assetEditor != null)
{
EditorGUILayout.LabelField($"{_selectedAsset.name}", EditorStyles.boldLabel);
EditorGUILayout.Space();
// 직접 만든 창에서는 라벨 너비가 기본값에 고정되어 긴 필드 이름이 잘리므로, 오른쪽 패널 너비에 맞춘 뒤 되돌린다
float previousLabelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = Mathf.Max(MIN_LABEL_WIDTH, (position.width - LIST_WIDTH) * LABEL_WIDTH_RATIO);
_assetEditor.OnInspectorGUI();
EditorGUIUtility.labelWidth = previousLabelWidth;
}
else
{
EditorGUILayout.HelpBox("Select an asset from the list on the left.", MessageType.Info);
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}
}
}