Skip to main content
fixed typos, added code markdown
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

I made script that Draws itdraws its children in DrawGizmosDrawGizmos. It works pretty well but the selection doesntdoesn't really work. When you try to select individual objectobjects it always selects the parent. This is a partial solution but in case someone can also help with the selection it would be appreciated. 

enter image description herescreenshot

I made script that Draws it children in DrawGizmos. It works pretty well but the selection doesnt really work. When you try to select individual object it always selects the parent. This is partial solution but in case someone can also help with the selection it would be appreciated.enter image description here

I made script that draws its children in DrawGizmos. It works pretty well but the selection doesn't really work. When you try to select individual objects it always selects the parent. This is a partial solution but in case someone can also help with the selection it would be appreciated. 

screenshot

Source Link

I made script that Draws it children in DrawGizmos. It works pretty well but the selection doesnt really work. When you try to select individual object it always selects the parent. This is partial solution but in case someone can also help with the selection it would be appreciated.enter image description here

[ExecuteInEditMode]
public class DrawColliders2D : MonoBehaviour
{
    [SerializeField]
    protected bool _filledColliders;

    [SerializeField] protected Color _usedColor = Color.yellow;
    
    private void OnDrawGizmos()
    {
        var colliders = GetComponentsInChildren<Collider2D>();

        
        for (int i = 0; i < colliders.Length; ++i)
        {
            var collider = colliders[i];
            if(Selection.activeObject == collider.gameObject) continue;
            Type t = collider.GetType();
            switch(t.Name)
            {
                case nameof(CircleCollider2D):
                    DrawCircleCollider((CircleCollider2D)collider, _usedColor, _filledColliders);
                break;
                case nameof(BoxCollider2D):
                    
                    DrawBoxCollider((BoxCollider2D)collider, _usedColor, _filledColliders);
                    break;
                default:
                    throw new NotSupportedException("Not supported for " + t.Name);
            }
        }
    }
    
    private static void DrawCircleCollider(CircleCollider2D cCol, Color color, bool fill = false)
    {
        var lastColor = Gizmos.color;
        var last = Gizmos.matrix;
        Gizmos.color = color;
        var scale = cCol.transform.lossyScale; 
        Gizmos.matrix = Matrix4x4.TRS(cCol.transform.position, cCol.transform.rotation, new Vector3(scale.x, scale.y, scale.z * 0.001f));
        var center = Vector3.zero;
        if (fill)
        {
            Gizmos.DrawSphere(center, cCol.radius);
        }
        else
        {
            Gizmos.DrawWireSphere(center, cCol.radius);
        }

        Gizmos.matrix = last;
        Gizmos.color = lastColor;
    }

    public static void DrawBoxCollider(BoxCollider2D bCol, Color color, bool fill = false)
    {
        var lastColor = Gizmos.color;
        var last = Gizmos.matrix;
        Gizmos.color = color;
        var scale = bCol.transform.lossyScale; 
        Gizmos.matrix = Matrix4x4.TRS(bCol.transform.position, bCol.transform.rotation, new Vector3(scale.x, scale.y, scale.z * 0.001f));
        var center = Vector3.zero;
        if (fill)
        {
            Gizmos.DrawCube(center, bCol.size);
        }
        else
        {
            var halfSize = (Vector3)bCol.size * 0.5f;

            var c0 = center - halfSize;
            var c1 = c0 + new Vector3(bCol.size.x, 0, 0);
            var c2 = center + halfSize;
            var c3 = c2 - new Vector3(bCol.size.x, 0, 0);
            Vector3[] points = new[] { c0, c1, c1, c2, c2, c3, c3, c0};
            Gizmos.DrawLineList(points);
        }

        Gizmos.matrix = last;
        Gizmos.color = lastColor;
    }
}