- Home /
Display collider in scene view without object selected
What the title says :)
Is it possible to display all the colliders in the scene view without having to select them? I use them a lot to define waypoints and having to select them all to see where they are in relation to each other makes it really hard...
Maybe there is just another way to do this?
PS.sorry if I wasnt specific but I want to draw them while I'm editing and not during 'Play' mode
Answer by huulong · Jan 27, 2016 at 05:48 AM
UPDATE: for 2D colliders an option has been added in the editor Physics2D settings (probably since Unity 5.4) to Always Show Colliders. You can even tune the colors and other parameters, but not distinguish triggers and non-triggers. Note that these are global settings, so you may still have some use for my scripts below.
This answer is for PolygonCollider2D and EdgeCollider2D only.
As Statement said, this approach requires to attach a special debug script to the game object you are interested in, so you may have to look for a unique manager/drawer object depending on how many objects you are debugging at the same time.
I've modified @AdamZbadam's script to support transformations. In addition, I've integrated editor code from http://stackoverflow.com/questions/29819697/manually-edit-unity3d-collider-coordinates to also provide the ability to modify the collider vertex coordinates manually.
Since the question is about viewing the colliders, here is the MonoBehaviour script first, for a PolygonCollider2D:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(PolygonCollider2D))]
public class EditPolygonCollider2D : MonoBehaviour
{
public PolygonCollider2D polygonCollider2D { get { return m_PolygonCollider2D; } }
PolygonCollider2D m_PolygonCollider2D;
/// <summary>
/// Should the collider be visible even when the game object is not selected? (experimental: requires no rotation in the hierarchy and local scale only)
/// </summary>
[SerializeField] bool alwaysShowCollider;
void Awake () {
m_PolygonCollider2D = GetComponent<PolygonCollider2D>();
}
void OnDrawGizmos() {
if (alwaysShowCollider) {
Vector2[] points = m_PolygonCollider2D.points;
Gizmos.color = Color.blue;
// for every point (except for the last one), draw line to the next point
for(int i = 0; i < points.Length-1; i++)
{
GizmosUtil.DrawLocalLine(transform, (Vector3) points[i], (Vector3) points[i+1]);
}
// for polygons, close with the last segment
GizmosUtil.DrawLocalLine(transform, (Vector3) points[points.Length - 1], (Vector3) points[0]);
}
}
}
where DrawLocalLine
is defined in my custom utils (but you can inline it if you prefer):
public static class GizmosUtil {
/// <summary>
/// Draw a line with local coordinates, with current gizmos parameters
/// </summary>
/// <param name="p1">Local 1st coordinates of the line.</param>
/// <param name="p2">Local 2nt coordinates of the line.</param>
public static void DrawLocalLine (Transform tr, Vector3 p1, Vector3 p2) {
Gizmos.DrawLine(tr.TransformPoint(p1), tr.TransformPoint(p2));
}
}
By checking alwaysShowCollider
, the collider will appear in blue even when the object is not selected. You can also check whether the object is actually selected or not to avoid having an overdraw of blue on top of the native Unity green collider draw (which also shows triangulation by the way).
For other colliders you would have to adapt the EditXXXCollider script. Gizmos are 3D so even 3D colliders should be fine, but you have to decide how to draw more complex shapes (Capsule collider for instance). Other colliders do not need an extra script to manually edit coordinates, or such a script would not be relevant (mesh collider for instance).
UPDATE: Unity 5.4.0 has reintroduced manual coordinate editing for PolygonCollider2D and EdgeCollider2D. Besides, you can edit them directly in Normal mode, you don't need Debug mode. This makes the code below obsolete, but you can still use it as a base if you want to create a more advanced point editor.
Old editor code (optional, to allow coordinate edit only):
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(EditPolygonCollider2D))]
public class EditPolygonCollider2DEditor : Editor {
public override void OnInspectorGUI() {
DrawDefaultInspector();
PolygonCollider2D collider = ((EditPolygonCollider2D) target).polygonCollider2D;
var points = collider.points;
for (int i = 0; i < points.Length; i++){
points[i] = UnityEditor.EditorGUILayout.Vector2Field(i.ToString(), points[i]);
}
collider.points = points;
UnityEditor.EditorUtility.SetDirty(collider);
}
}
how can i make this script to work for $$anonymous$$eshcollider? i want to be able to see in the scene view the mesh collider of a plane i created.
please help me!
If you want to apply this code to meshes to would need to draw every edge of the mesh manually... I suggest using a mesh drawing function in this case. See http://answers.unity3d.com/questions/230559/can-i-draw-mesh-in-editorwindow-ongui.html although Graphics.Draw$$anonymous$$esh should work in your case, and is less expensive when applied to many meshes. $$anonymous$$eep the structure of the scripts above and replace the drawing iteration with Draw$$anonymous$$esh. Get the collider mesh with shared$$anonymous$$esh (see http://docs.unity3d.com/ScriptReference/$$anonymous$$eshCollider.html).
However, these are truly meshes and use materials ins$$anonymous$$d of just a color. Ideally you would need a material with a wireframe shader, or maybe an outline shader depending on what you want. If you are not familiar with shaders you may have to download some ready-made shaders.
Sorry to post this as a comment, maybe someone else can dig the thing and post a complete answer if I don't do it myself.
thank you, i will take a look at this for sure Edit: I made it work, thanks for the recomendations u were right on the graphics.Draw$$anonymous$$esh.
Thanks!!
Answer by Loius · Dec 23, 2012 at 03:10 AM
The OnDrawGizmos function lets you draw objects when they're not selected (as opposed to OnDrawGizmosSelected). You can draw a Gizmo box with the collider's bounds, or get more inventive and add connecting lines or what-have-you.
Check the Script Reference for details (I haven't implemented this specific ability before) - use the Gizmos class to draw and you can click on the drawn objects to select them, too.
sorry if I wasnt specific but I want to draw them while I'm editing and not during 'Play' mode
Answer by Ultroman · Feb 23, 2018 at 09:21 AM
I wrote a script/component which can show all capsule, box and sphere colliders present on a gameobject, selected or not, with an option to include colliders on its children. Very useful when playing around with bones for e.g. a ragdoll and such. https://ultromanthetacoman.wordpress.com/2018/02/23/draw-unselected-colliders-in-unity/
Answer by Statement · Dec 23, 2012 at 03:14 AM
Yeah, you should be able to render the mesh or provide other meshes for other shapes.
I cant remember if you can use OnSceneGUI with objects unselected, but you could add a helper game object into your scene that renders all of your colliders of interest. You can set a tag for the game object to be an "EditorOnly" object - those will get stripped out of your final build. The idea with that approach would be that you'd have one centralized object in your scene that deals with rendering all of the colliders, to avoid having to attach a script to every single collider.
http://docs.unity3d.com/Documentation/ScriptReference/Editor.OnSceneGUI.html
http://docs.unity3d.com/Documentation/ScriptReference/MeshCollider-sharedMesh.html
http://docs.unity3d.com/Documentation/ScriptReference/Graphics.DrawMesh.html
http://docs.unity3d.com/Documentation/ScriptReference/Graphics.DrawMeshNow.html
Cool, if you just want a quick fix to the issue you could also just make a menu button that will create a new game object parented to the object with the collider, with a renderer. You can also get the primitive objects with http://docs.unity3d.com/Documentation/ScriptReference/GameObject.CreatePrimitive.html (you'd likely want to strip the collider off them though and change the material to something more suitable)
Also remember to keep track of these objects so you can delete them when you want to hide them. You can also set the save flags to not save them. http://docs.unity3d.com/Documentation/ScriptReference/HideFlags.HideAndDontSave.html
Answer by georgiegrace · May 20, 2018 at 11:39 AM
Thank you @Ultroman - this is exactly what I was looking for :-)
Your answer
Follow this Question
Related Questions
Make hidden gizmos invisible 0 Answers
Turn Off Camera Frustum-Indicators In Scene View 2 Answers
Internal collisions 1 Answer
Scene View perspective camera doesn't show girds and other gizmos 0 Answers
How can I do this with gizmos? 0 Answers