-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColliderVisualizerWindow.cs
More file actions
329 lines (264 loc) · 10.9 KB
/
ColliderVisualizerWindow.cs
File metadata and controls
329 lines (264 loc) · 10.9 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Tool window for visualizing colliders and triggers in the scene.
//
// Updated: 2021-09-27
/* MIT License
*
* Copyright (c) 2021 Jonatan Johansson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class ColliderVisualizerWindow : EditorWindow {
private const string MenuItemPath = "Tools/Collider Visualizer";
private const string StatePrefsKey = nameof(ColliderVisualizerWindow) + "_state";
private static ColliderVisualizerWindow window;
[SerializeField] private bool showColliders = true;
[SerializeField] private bool showTriggers = true;
[SerializeField] private bool showDisabled;
[SerializeField] private bool layerFilterFoldout;
[SerializeField] private LayerMask layerFilter = -1;
[SerializeField] private List<string> tags = new List<string>();
[SerializeField] private bool colliderTypeFoldout;
[SerializeField] private bool showBoxColliders = true;
[SerializeField] private bool showSphereColliders = true;
[SerializeField] private bool showCapsuleColliders = true;
[SerializeField] private bool showMeshColliders;
[SerializeField] private bool showConvexMeshColliders;
[SerializeField] private Color colliderColor = new Color(0.18f, 0.81f, 0.17f);
[SerializeField] private Color triggerColor = Color.yellow;
[SerializeField] private bool fill = true;
[SerializeField, Range(0, 1)] private float fillOpacity = 0.1f;
private SerializedObject serializedObject;
private Vector2 scrollPosition;
[MenuItem(MenuItemPath)]
private static void ShowWindow() {
window = GetWindow<ColliderVisualizerWindow>();
window.titleContent = new GUIContent("Collider Visualizer");
window.Show();
}
private void OnEnable() {
window = this;
serializedObject = new SerializedObject(this);
if (EditorPrefs.HasKey(StatePrefsKey))
JsonUtility.FromJsonOverwrite(EditorPrefs.GetString(StatePrefsKey), this);
}
private void OnDisable() {
EditorPrefs.SetString(StatePrefsKey, JsonUtility.ToJson(this));
}
private void OnGUI() {
serializedObject.UpdateIfRequiredOrScript();
EditorGUI.BeginChangeCheck();
ToolbarGUI();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
DrawFiltersGUI();
DrawColorsGUI();
EditorGUILayout.EndScrollView();
if (EditorGUI.EndChangeCheck())
SceneView.RepaintAll();
serializedObject.ApplyModifiedProperties();
}
private void Reset() {
showColliders = true;
showTriggers = true;
showBoxColliders = true;
showSphereColliders = true;
showCapsuleColliders = true;
showMeshColliders = false;
showConvexMeshColliders = false;
layerFilter = -1;
tags.Clear();
colliderColor = new Color(0.18f, 0.81f, 0.17f);
triggerColor = Color.yellow;
fill = true;
fillOpacity = 0.1f;
}
private void ToolbarGUI() {
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Reset", EditorStyles.toolbarButton))
Reset();
EditorGUILayout.EndHorizontal();
}
private void DrawColorsGUI() {
EditorGUILayout.LabelField("Colors", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
DrawProperty(nameof(colliderColor));
DrawProperty(nameof(triggerColor));
using (new EditorGUI.DisabledScope(!DrawProperty(nameof(fill)).boolValue))
DrawProperty(nameof(fillOpacity));
EditorGUILayout.EndVertical();
}
private void DrawFiltersGUI() {
EditorGUILayout.LabelField("Filters", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
DrawProperty(nameof(showColliders));
DrawProperty(nameof(showTriggers));
DrawProperty(nameof(showDisabled));
// Collider type filter foldout
colliderTypeFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(colliderTypeFoldout, "Collider Types");
if (colliderTypeFoldout) {
DrawProperty(nameof(showBoxColliders), new GUIContent("Box"));
DrawProperty(nameof(showSphereColliders), new GUIContent("Sphere"));
DrawProperty(nameof(showCapsuleColliders), new GUIContent("Capsule"));
DrawProperty(nameof(showMeshColliders), new GUIContent("Mesh"));
DrawProperty(nameof(showConvexMeshColliders), new GUIContent("Convex Mesh*", "The normal mesh will be displayed"));
}
EditorGUILayout.EndFoldoutHeaderGroup();
// Layer filter foldout
layerFilterFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(layerFilterFoldout, "Layers");
if (layerFilterFoldout) {
SerializedProperty layersFilterProperty = serializedObject.FindProperty(nameof(layerFilter));
// Quick select buttons
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("All")) layersFilterProperty.intValue = -1;
if (GUILayout.Button("None")) layersFilterProperty.intValue = 0;
EditorGUILayout.EndHorizontal();
// Layer mask checkboxes
EditorGUI.BeginChangeCheck();
int concatenatedLayersMask = InternalEditorUtility.LayerMaskToConcatenatedLayersMask(layerFilter);
string[] layers = InternalEditorUtility.layers;
for (int i = 0; i < layers.Length; i++) {
if (EditorGUILayout.Toggle(layers[i], (concatenatedLayersMask & (1 << i)) != 0))
concatenatedLayersMask |= 1 << i;
else
concatenatedLayersMask &= ~(1 << i);
}
if (EditorGUI.EndChangeCheck())
layersFilterProperty.intValue =
InternalEditorUtility.ConcatenatedLayersMaskToLayerMask(concatenatedLayersMask);
}
EditorGUILayout.EndFoldoutHeaderGroup();
// Tag filter
DrawProperty(nameof(tags));
EditorGUILayout.EndVertical();
}
private SerializedProperty DrawProperty(string propertyName, GUIContent label = null) {
SerializedProperty property = serializedObject.FindProperty(propertyName);
if (label != null)
EditorGUILayout.PropertyField(property, label, true);
else
EditorGUILayout.PropertyField(property, true);
return property;
}
private Color GetColor(Collider target, bool isFill = false) {
Color color = target.isTrigger ? triggerColor : colliderColor;
if (isFill)
color.a = fillOpacity;
return color;
}
private bool CheckFilter(Collider target) {
if (!showDisabled && !target.enabled)
return false;
bool isTrigger = target.isTrigger;
if (isTrigger && !showTriggers || !isTrigger && !showColliders)
return false;
if ((layerFilter.value & (1 << target.gameObject.layer)) == 0)
return false;
if (tags.Count > 0 && !tags.Contains(target.tag))
return false;
return true;
}
// I couldn't get any combination of flags to do what I want here. Setting all bits seems to work though for some reason...
[DrawGizmo((GizmoType)~0)]
private static void DrawGizmo(Collider target, GizmoType gizmoType) {
if (window == null)
return;
if (!window.CheckFilter(target))
return;
switch (target) {
case BoxCollider box: {
if (!window.showBoxColliders)
break;
Gizmos.matrix = target.transform.localToWorldMatrix;
if (window.fill) {
Gizmos.color = window.GetColor(target, true);
Gizmos.DrawCube(box.center, box.size);
}
Gizmos.color = window.GetColor(target);
Gizmos.DrawWireCube(box.center, box.size);
break;
}
case SphereCollider sphere: {
if (!window.showSphereColliders)
break;
Gizmos.matrix = target.transform.localToWorldMatrix;
if (window.fill) {
Gizmos.color = window.GetColor(target, true);
Gizmos.DrawSphere(sphere.center, sphere.radius);
}
Gizmos.color = window.GetColor(target);
Gizmos.DrawWireSphere(sphere.center, sphere.radius);
break;
}
case CapsuleCollider capsule: {
if (!window.showCapsuleColliders)
break;
Handles.matrix = target.transform.localToWorldMatrix;
Handles.color = window.GetColor(target);
DrawWireCapsule(capsule.center, capsule.height, capsule.radius);
// We're not drawing a fill for capsules, as there is no built-in function for that. Just drawing a
// scaled version of the built-in capsule mesh wouldn't work very well, as the scaling would cause
// stretching. So, one would have to generate a capsule mesh based on the height and radius, and draw
// that with a flat color.
break;
}
case MeshCollider mesh: {
if (mesh.sharedMesh == null)
break;
if (window.showMeshColliders && !mesh.convex || window.showConvexMeshColliders && mesh.convex) {
Gizmos.matrix = target.transform.localToWorldMatrix;
Gizmos.color = window.GetColor(target);
// There is no way to get the generated convex mesh from a mesh collider. So the normal mesh will
// have to do for both convex, and non-convex, mesh colliders.
Gizmos.DrawWireMesh(mesh.sharedMesh);
}
break;
}
}
}
// There is no built in function for drawing a wireframe capsule.
private static void DrawWireCapsule(Vector3 center, float height, float radius) {
float cylinderHeight = Mathf.Max(0f, height - radius * 2f);
float pointOffset = cylinderHeight / 2f;
// Top
Vector3 topPoint = Vector3.up * pointOffset;
Handles.DrawWireArc(center + topPoint, Vector3.left, Vector3.back, -180f, radius);
Handles.DrawWireArc(center + topPoint, Vector3.back, Vector3.left, 180f, radius);
Handles.DrawWireDisc(center + topPoint, Vector3.up, radius);
// Bottom
Vector3 bottomPoint = Vector3.down * pointOffset;
Handles.DrawWireArc(center + bottomPoint, Vector3.left, Vector3.back, 180f, radius);
Handles.DrawWireArc(center + bottomPoint, Vector3.back, Vector3.left, -180f, radius);
Handles.DrawWireDisc(center + bottomPoint, Vector3.up, radius);
// Middle
if (cylinderHeight > 0f) {
Handles.DrawLine(center + new Vector3(radius, bottomPoint.y, 0),
center + new Vector3(radius, topPoint.y, 0));
Handles.DrawLine(center + new Vector3(-radius, bottomPoint.y, 0),
center + new Vector3(-radius, topPoint.y, 0));
Handles.DrawLine(center + new Vector3(0, bottomPoint.y, radius),
center + new Vector3(0, topPoint.y, radius));
Handles.DrawLine(center + new Vector3(0, bottomPoint.y, -radius),
center + new Vector3(0, topPoint.y, -radius));
}
}
}