forked from alvivar/GhostGrid
/
GhostGridEditor.cs
281 lines (201 loc) · 7.3 KB
/
GhostGridEditor.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Linq;
/// <summary>
/// Editor controls for GhostGrid.
/// </summary>
using System.IO;
[CustomEditor(typeof(GhostGrid))]
public class GhostGridEditor : Editor
{
private GhostGrid grid;
private string message;
Transform[] children;
// prefabs information
FileInfo[] prefabsFileInfo;
bool drawCursor = false;
public void OnEnable()
{
grid = target as GhostGrid;
children = grid.GetComponentsInChildren<Transform>();
message = "";
DirectoryInfo dir = new DirectoryInfo("Assets/Resources");
prefabsFileInfo = dir.GetFiles("*.prefab");
}
Vector2 scrollPosition = Vector2.zero;
public override void OnInspectorGUI()
{
//////
GUILayout.Label("");
DrawDefaultInspector();
GUILayout.Label("");
GUILayout.Label("SNAPPING");
GUILayout.BeginHorizontal();
// Snap once button
if (GUILayout.Button("Snap Once", GUILayout.ExpandWidth(false)))
{
message = "Grid snapped!";
if (grid.autoSnapEnabled)
{
grid.autoSnapEnabled = false;
}
else
{
grid.SnapAll();
}
}
// Auto snap button
if (GUILayout.Button(grid.autoSnapEnabled ? "Disable Auto Snap" : "Enable Auto Snap", GUILayout.ExpandWidth(false)))
{
message = "Changed!";
grid.autoSnapEnabled = !grid.autoSnapEnabled;
if (grid.autoSnapEnabled)
grid.SnapAll();
}
GUILayout.EndHorizontal();
GUILayout.Label("");
GUILayout.Label("OPTIMIZATIONS");
// Rename button
GUILayout.BeginHorizontal();
if (GUILayout.Button("Rename children", GUILayout.ExpandWidth(false)))
{
message = grid.RenameChildren() + " children renamed!";
}
GUILayout.EndHorizontal();
// Exclude overlapped button
GUILayout.BeginHorizontal();
if (GUILayout.Button("Exclude Overlapped Children", GUILayout.ExpandWidth(false)))
{
message = grid.ExcludeOverlappedChildren() + " overlapped children deleted!";
}
GUILayout.EndHorizontal();
// Optimize colliders button
GUILayout.BeginHorizontal();
if (GUILayout.Button("Turn Off Unneeded 2D Colliders", GUILayout.ExpandWidth(false)))
{
message = grid.TurnOffUnneededColliders2D() + " unneeded colliders were turned off!";
}
GUILayout.EndHorizontal();
// All optimizations colliders button
GUILayout.BeginHorizontal();
if (GUILayout.Button("ALL ^", GUILayout.ExpandWidth(false)))
{
message =
grid.RenameChildren() + " children renamed!" + "\n" +
grid.ExcludeOverlappedChildren() + " overlapped children deleted!" + "\n" +
grid.TurnOffUnneededColliders2D() + " unneeded colliders were turned off!";
}
GUILayout.EndHorizontal();
GUILayout.Label("");
GUILayout.BeginHorizontal();
if (GUILayout.Button("#experimental Polygon Builder", GUILayout.ExpandWidth(false)))
{
message = grid.RebuildPolygon() + "";
}
GUILayout.EndHorizontal();
drawAssetPreview ();
// Status label
GUILayout.Label("");
GUILayout.Label(grid.autoSnapEnabled ? "Auto Snap Running!" : "Auto Snap Disabled.");
if (message.Length > 0)
GUILayout.Label(message);
// Credits
GUILayout.Label("");
GUILayout.Label("GhostGrid v0.1.3.5 by @matnesis");
}
void drawAssetPreview(){
EditorGUILayout.BeginVertical(GUILayout.MinHeight(128.0f));
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
// create a button for each image loaded in, 4 buttons in width
// calls the handler when a new image is selected.
int counter = 0;
int n = 10; // prefabs
prefabsFileInfo.Select(f => f.FullName).ToArray();
foreach (FileInfo f in prefabsFileInfo)
{
GameObject prefab = Resources.Load (f.Name.Split('.')[0]) as GameObject;
++counter;
if(GUILayout.Button(f.Name, GUILayout.Height(27)))
{
grid.mainPrefab = prefab;
}
}
GUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}
void OnSceneGUI()
{
Event e = Event.current;
// We use hotControl to lock focus onto the editor (to prevent deselection)
int controlID = GUIUtility.GetControlID (FocusType.Passive);
Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
if (drawCursor) {
drawGridCursor ();
}
switch (Event.current.GetTypeForControl (controlID)) {
case EventType.KeyDown:
if (e.keyCode == KeyCode.Y) {
drawCursor = true;
}
break;
case EventType.KeyUp:
if (e.keyCode == KeyCode.Y) {
drawCursor = false;
}
break;
}
if(!drawCursor){
return;
}
switch (Event.current.GetTypeForControl (controlID)) {
case EventType.MouseDown:
drawGridCursor ();
drawGridRectangle();
GUIUtility.hotControl = controlID;
PlaceObject();
Event.current.Use();
break;
case EventType.MouseUp:
GUIUtility.hotControl = 0;
Event.current.Use();
break;
}
HandleUtility.Repaint ();
}
void drawGridRectangle(){
float bottomY = children.Cast<Transform>().OrderBy(t=>t.position.y).First().position.y; // bottom
float topY = children.Cast<Transform>().OrderBy(t=>t.position.y).Last().position.y; // top
float leftX = children.Cast<Transform>().OrderBy(t=>t.position.x).First().position.x; // bottom
float rightX = children.Cast<Transform>().OrderBy(t=>t.position.x).Last().position.x; // top
Debug.DrawLine(new Vector3(leftX, topY, 0f), new Vector3(rightX, topY, 0f), Color.yellow);
Debug.DrawLine(new Vector3(rightX, bottomY, 0f), new Vector3(rightX, topY, 0f), Color.blue);
Debug.DrawLine(new Vector3(leftX, bottomY, 0f), new Vector3(leftX, topY, 0f), Color.black);
Debug.DrawLine(new Vector3(leftX, bottomY, 0f), new Vector3(rightX, bottomY, 0f), Color.red);
}
void drawGridCursor(){
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
// draw a cursor of the size of the gridSize
Vector3 mousePos = new Vector3(ray.origin.x, ray.origin.y, 0f);
mousePos = GhostGrid.GetSnapVector (mousePos, grid.gridSize);
// set the paddings to center mouse
mousePos = mousePos - new Vector3 (grid.gridSize / 2, grid.gridSize / 2);
//draw
Handles.DrawLine(mousePos,mousePos + new Vector3(grid.gridSize, 0f, 0f));
Handles.DrawLine(mousePos,mousePos + new Vector3(0f, grid.gridSize, 0f));
Handles.DrawLine(mousePos + new Vector3(0f, grid.gridSize, 0f),mousePos + new Vector3(grid.gridSize, grid.gridSize, 0f));
Handles.DrawLine(mousePos + new Vector3(grid.gridSize, 0f, 0f) ,mousePos + new Vector3(grid.gridSize, grid.gridSize, 0f));
}
void PlaceObject()
{
if (grid.mainPrefab == null) {
return;
}
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
Vector3 mousePos = new Vector3(ray.origin.x, ray.origin.y, 0f);
mousePos = GhostGrid.GetSnapVector (mousePos, grid.gridSize);
GameObject go = (GameObject)GameObject.Instantiate(grid.mainPrefab, mousePos, Quaternion.identity);
go.transform.parent = grid.transform;
}
}
#endif