-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuGUIGuidelines.cs
223 lines (200 loc) · 7.88 KB
/
uGUIGuidelines.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
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR
namespace Syy.uGUIGuidelines
{
public class uGUIGuidelines : EditorWindow
{
[MenuItem("Window/uGUI-Guidelines")]
public static void Open()
{
GetWindow<uGUIGuidelines>("Guidelines");
}
static Dictionary<int,Status> _drawInfo = new Dictionary<int, Status>();
static uGUIGuidelineComponent guidlineDrawer;
static Color guidlineColor = new Color(80/255f, 255/ 255f, 22/ 255f);
static uGUIGuidelines window;
const int WIDTH = 16;
const int HEIGHT = 16;
void OnEnable()
{
window = this;
DestoryGuidlineComponent();
CreateGuidlineComponent();
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
void OnDisable()
{
EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyGUI;
DestoryGuidlineComponent();
window = null;
}
void CreateGuidlineComponent()
{
var obj = new GameObject("uGUI-Guideline-Drawer");
guidlineDrawer = obj.AddComponent<uGUIGuidelineComponent>();
obj.hideFlags = HideFlags.DontSaveInEditor;
}
void DestoryGuidlineComponent()
{
if(guidlineDrawer != null)
{
GameObject.DestroyImmediate(guidlineDrawer.gameObject);
}
}
void OnGUI()
{
guidlineColor = EditorGUILayout.ColorField("Color",guidlineColor);
var targets = _drawInfo.Where(data => data.Value.Target != null && data.Value.IsDraw);
EditorGUILayout.LabelField("Draw Targets");
using(new EditorGUILayout.VerticalScope("box"))
{
if(targets.Count() == 0)
{
EditorGUILayout.LabelField("Check RectTransform's HierarchyGUI toggle box or click \"c\" button");
} else {
foreach (var item in targets)
{
EditorGUILayout.LabelField(item.Value.Target.name);
}
}
}
}
static void OnHierarchyGUI(int instanceId, Rect selectionRect)
{
var target = EditorUtility.InstanceIDToObject(instanceId) as GameObject;
if (target == null)
{
return;
}
if(!(target.transform is RectTransform))
{
return;
}
Status status;
if(_drawInfo.ContainsKey(instanceId))
{
status = _drawInfo[instanceId];
if(status.Target == null)
{
_drawInfo.Remove(instanceId);
return;
}
} else {
status = new Status();
status.IsDraw = false;
status.Target = (RectTransform)target.transform;
_drawInfo[instanceId] = status;
}
//switch children draw
var childSwitchRect = selectionRect;
childSwitchRect.x = childSwitchRect.xMax - WIDTH;
childSwitchRect.width = WIDTH;
childSwitchRect.height = HEIGHT;
if(GUI.Button(childSwitchRect, "c"))
{
List<RectTransform> childList = new List<RectTransform>();
GetChildren(status.Target, ref childList);
var drawChildTargets = childList.Where(data => {
int id = data.gameObject.GetInstanceID();
return _drawInfo.ContainsKey(id) && _drawInfo[id].IsDraw;
});
if(drawChildTargets.Count() == 0)
{
//draw all child
foreach (var child in childList)
{
int id = child.gameObject.GetInstanceID();
var newStatus = new Status();
newStatus.IsDraw = true;
newStatus.Target = child;
_drawInfo[id] = newStatus;
}
} else {
//remove all draw target child
foreach (var child in drawChildTargets)
{
int id = child.gameObject.GetInstanceID();
_drawInfo[id].IsDraw = false;
}
}
ForceRepaint();
}
//switch own draw
var ownSwitchRect = selectionRect;
ownSwitchRect.x = ownSwitchRect.xMax - WIDTH * 2 - 3;
ownSwitchRect.width = WIDTH;
ownSwitchRect.height = HEIGHT;
EditorGUI.BeginChangeCheck();
status.IsDraw = EditorGUI.Toggle(ownSwitchRect, status.IsDraw);
if(EditorGUI.EndChangeCheck())
{
ForceRepaint();
}
}
static void GetChildren(RectTransform obj, ref List<RectTransform> allChildren)
{
var children = obj.GetComponentInChildren<RectTransform>();
if (children.childCount == 0)
{
return;
}
foreach (RectTransform child in children)
{
allChildren.Add(child);
GetChildren(child, ref allChildren);
}
}
static void ForceRepaint()
{
SceneView.RepaintAll();
window?.Repaint();
}
[DrawGizmo(GizmoType.NonSelected | GizmoType.Active)]
static void DrawGizmo(uGUIGuidelineComponent drawer, GizmoType gizmoType)
{
var targets = _drawInfo.Where(data => data.Value.Target != null && data.Value.IsDraw);
foreach (var target in targets)
{
if (target.Value.IsDraw)
{
var t = target.Value.Target;
var position = t.position;
var localPosition = t.localPosition;
var sizeDelta = t.sizeDelta;
var pivot = t.pivot;
var lossyScale = t.lossyScale;
var xOffset = position.x - localPosition.x;
var yOffset = position.y - localPosition.y;
var x1 = (localPosition.x - (sizeDelta.x * pivot.x) * lossyScale.x) + xOffset;
var x2 = (localPosition.x + (sizeDelta.x * (1f - pivot.x) * lossyScale.x)) + xOffset;
var y1 = (localPosition.y - (sizeDelta.y * pivot.y) * lossyScale.y) + yOffset;
var y2 = (localPosition.y + (sizeDelta.y * (1f - pivot.y) * lossyScale.y)) + yOffset;
var max = 100000f;
var min = -100000f;
Gizmos.color = guidlineColor;
Gizmos.DrawLine(new Vector3(x1, min, 0f), new Vector3(x1, max, 0f));
Gizmos.DrawLine(new Vector3(x2, min, 0f), new Vector3(x2, max, 0f));
Gizmos.DrawLine(new Vector3(min, y1, 0f), new Vector3(max, y1, 0f));
Gizmos.DrawLine(new Vector3(min, y2, 0f), new Vector3(max, y2, 0f));
}
}
}
public class Status
{
public RectTransform Target;
public bool IsDraw;
}
[AddComponentMenu("")] //HACK! Disable addition from Add Component Button in Inspector
public class uGUIGuidelineComponent : MonoBehaviour
{
void Awake()
{
}
}
}
}
#endif