Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by castor · Dec 23, 2012 at 03:04 AM · colliderdisplayscene viewgizmos

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
5

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.

Always Show Colliders settings

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);
     }
 
 }



physics2dsettings-gizmos-always-show-colliders.png (10.5 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vanidash-Studios · Mar 22, 2016 at 09:04 PM 0
Share

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!

avatar image huulong · Mar 22, 2016 at 11:55 PM 1
Share

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.

avatar image Vanidash-Studios huulong · Mar 23, 2016 at 01:28 AM 0
Share

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!!

avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image castor · Dec 23, 2012 at 03:18 AM 0
Share

sorry if I wasnt specific but I want to draw them while I'm editing and not during 'Play' mode

avatar image Loius · Dec 25, 2012 at 06:05 AM 1
Share

Right, that's what OnDrawGizmos does.

avatar image
2

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/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image castor · Dec 23, 2012 at 03:20 AM 0
Share

Cool, going to research this! Thanks for the help

avatar image Statement · Dec 23, 2012 at 03:26 AM 0
Share

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

avatar image
1

Answer by georgiegrace · May 20, 2018 at 11:39 AM

Thank you @Ultroman - this is exactly what I was looking for :-)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges