Colliders in scene view

Hey guys, when you select an object with a box collider in the hierarchy then you can see the green outline in the scene view right? But if you select any other object then the green outline disappears. Is there a way to make it so it stays visible all the time?

If not then is there a way to create my own object and mimic the green outline behaviour in the scene view through code?

The green box you see is the collider of that GameObject, if an object does not have a collider attached, then you won’t see the green box. If you do have a collider attached, but you still don’t see the box - make sure that its dropped down in the inspector (the little arrow).

Alternatively, you can make the box yourself by using Gizmos:

https://docs.unity3d.com/Documentation/ScriptReference/Gizmos.html

The ‘OnDrawGizmos’ function gets called at runtime - so whatever you place within that function, will run, even if the game isn’t playing. Take a look at the ‘DrawCube’.

Note, you won’t see any of these when in the actual game window (unless you have gizmos active in the game), but even if you build it out - you wont see it.

Another alternative, is that if you select everything in the scene (Cmd + A / Ctrl + A), you can then see all your colliders (or place all the objects in a gameEmpty. Then all you have to do is select that one gameEmpty to select all the objects.)

I think I got something workable. Create a new object and attach a box or edge collider to it and then add this script to it.

using UnityEngine;
using System.Collections;

public class s_Boundary : MonoBehaviour {

	void OnDrawGizmos() {
		EdgeCollider2D e = GetComponent<EdgeCollider2D>();
		BoxCollider2D b = GetComponent<BoxCollider2D>();
		Transform t = GetComponent<Transform>();

		// Draw EdgeColliders
		if (e != null) {
			int i = 0;
			foreach(Vector2 v in e.points){
				if (i != e.points.Length - 1) {  
					Vector3 start = new Vector3(v.x + t.position.x, v.y + t.position.y, 0f);
					Vector3 end = new Vector3(e.points[i+1].x + t.position.x, e.points[i+1].y + t.position.y, 0f);
					Gizmos.color = Color.yellow;
					Gizmos.DrawLine (start, end);
					i++;
				}
			}
		}

		// Draw BoxColliders
		if (b != null) { 
			Vector3 tl = new Vector3(t.position.x - (b.size.x / 2), t.position.y + (b.size.y / 2), 0f);
			Vector3 bl = new Vector3(t.position.x - (b.size.x / 2), t.position.y - (b.size.y / 2), 0f);
			Vector3 br = new Vector3(t.position.x + (b.size.x / 2), t.position.y - (b.size.y / 2), 0f);
			Vector3 tr = new Vector3(t.position.x + (b.size.x / 2), t.position.y + (b.size.y / 2), 0f);
			Gizmos.color = Color.red;
			Gizmos.DrawLine (tl, bl);
			Gizmos.DrawLine (bl, br);
			Gizmos.DrawLine (br, tr);
			Gizmos.DrawLine (tr, tl);
		}
	}
}

Now all of the box and edge colliders will be visible no matter which one is currently selected. :slight_smile:

Thanks for the code @Yeowza. I’ve optimized it so it also takes the BoxCollider2D offset into account.

using UnityEngine;
using System.Collections;

public class DrawColliders : MonoBehaviour {

	void OnDrawGizmos() {
         EdgeCollider2D e = GetComponent<EdgeCollider2D>();
         BoxCollider2D b = GetComponent<BoxCollider2D>();
         Transform t = GetComponent<Transform>();
 
         // Draw EdgeColliders
         if (e != null) {
             int i = 0;
             foreach(Vector2 v in e.points){
                 if (i != e.points.Length - 1) {  
                     Vector3 start = new Vector3(v.x + t.position.x, v.y + t.position.y, 0f);
                     Vector3 end = new Vector3(e.points[i+1].x + t.position.x, e.points[i+1].y + t.position.y, 0f);
                     Gizmos.color = Color.yellow;
                     Gizmos.DrawLine (start, end);
                     i++;
                 }
             }
         }
 
         // Draw BoxColliders

         float offsetX = b.offset.x;
         float offsetY = b.offset.y;

         if (b != null) { 
             Vector3 tl = new Vector3(t.position.x - (b.size.x / 2) + offsetX, t.position.y + (b.size.y / 2) + offsetY, 0f + offsetX);
             Vector3 bl = new Vector3(t.position.x - (b.size.x / 2) + offsetX, t.position.y - (b.size.y / 2) + offsetY, 0f + offsetX);
             Vector3 br = new Vector3(t.position.x + (b.size.x / 2) + offsetX, t.position.y - (b.size.y / 2) + offsetY, 0f + offsetX);
             Vector3 tr = new Vector3(t.position.x + (b.size.x / 2) + offsetX, t.position.y + (b.size.y / 2) + offsetY, 0f + offsetX);
             Gizmos.color = Color.red;
             Gizmos.DrawLine (tl, bl);
             Gizmos.DrawLine (bl, br);
             Gizmos.DrawLine (br, tr);
             Gizmos.DrawLine (tr, tl);
         }
     }

}

I added CircleColliders to this script too:

 // Draw Circe Colliders
        if (c != null)
        {
            Vector3 circleCenter = new Vector3(t.position.x, t.position.y + c.offset.y);
            Gizmos.color = Color.yellow;
            Gizmos.DrawWireSphere(circleCenter, c.radius);
        }