Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Feedback Feature request: Show solid colliders

Discussion in 'Editor & General Support' started by Marscaleb, Oct 9, 2020.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    When I'm trying to line up colliders on my prefabs and world geometry, it is a little hard to be certain exactly where the limits are. This is especially true with colliders that are much larger than the area I'm working with.

    It would be super handy if there was an option to make colliders have a semi-transparent color. So if I'm lining up a box collider with my world geometry. instead of having just the green wireframe where I have to keep spinning my camera around and then guessing how close it is, the box collider could be drawn as a semi-transparent box, and then I could see clearly where the edge of the box is; I could easily recognize what geometry is sticking past the collider and what is still behind it.
     
  2. Stephanommg

    Stephanommg

    Joined:
    Aug 18, 2014
    Posts:
    88
    Yes it is unbelievable that Unity still don't have an option to have a solid visualization of colliders.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,185
    You can easily write editor tools for this:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. public class ColliderGizmoDrawers
    3. {
    4.     [DrawGizmo(GizmoType.Selected | GizmoType.Active, typeof(BoxCollider))]
    5.     static void DrawGizmoForMyScript(BoxCollider col, GizmoType gizmoType)
    6.     {
    7.         Gizmo.matrix = col.transform.localToWorldMatrix;
    8.         Vector3 size = col.size;
    9.         Gizmo.DrawCube(Vector3.zero, size);
    10.         Gizmo.matrix = Matrix4x4.identity;
    11.     }
    12. }
    13. #endif
    Not 100% correct but not bothered to do so in notepad. This will draw box colliders as a solid box when selected.
     
    Last edited: Jul 21, 2023
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,185
    Okay I was curious as to how 'nice' I could make this, as if it were an actual Unity tool. Turns out there's lots of cool Editor API stuff to make it intuitive:
    upload_2023-7-21_22-4-19.png

    (Couldn't be assed making a gif).

    This is using the Overlays API. Relevant code below:

    Code (CSharp):
    1. // the visual element used in the overlay toolbar
    2. [EditorToolbarElement(SolidCollidersToolbarToggle.id, typeof(SceneView))]
    3. internal sealed class SolidCollidersToolbarToggle : EditorToolbarToggle
    4. {
    5.     public const string id = "Colliders/DrawSolidColliders";      
    6.  
    7.     public SolidCollidersToolbarToggle() : base()
    8.     {
    9.         text = "Draw Solid Colliders";
    10.         tooltip = "Toggles whether colliders should be drawn as solid or as wireframe.";
    11.         icon = (Texture2D)EditorGUIUtility.Load("d_BoxCollider Icon");
    12.         this.SetValueWithoutNotify(LBGEditorPreferences.DrawSolidColliders);
    13.         this.RegisterValueChangedCallback<bool>(HandleToggleValue);
    14.     }
    15.  
    16.     private void HandleToggleValue(ChangeEvent<bool> value)
    17.     {
    18.         LBGEditorPreferences.DrawSolidColliders = value.newValue;
    19.     }
    20. }
    21.  
    22. [Overlay(typeof(SceneView), "Collider Tools")]
    23. internal sealed class ColliderToolbar : ToolbarOverlay
    24. {
    25.     public ColliderToolbar() : base(SolidCollidersToolbarToggle.id) { }
    26. }
    27.  
    28. internal static class ColliderGizmosDrawer
    29. {
    30.     [DrawGizmo(GizmoType.Active | GizmoType.Selected, typeof(BoxCollider))]
    31.     public static void HandleDrawBoxColliderGizmo(BoxCollider collider, GizmoType gizmoType)
    32.     {
    33.         if (gizmoType.HasFlag(GizmoType.NotInSelectionHierarchy))
    34.         {
    35.             return;
    36.         }
    37.  
    38.         bool solid = LBGEditorPreferences.DrawSolidColliders;
    39.         Color c = Color.green;
    40.         c.a = 0.5f;
    41.         DrawGizmosUtilities.DrawBoxColliderGizmos(collider, c, solid);
    42.     }
    43. }
    44.  
    45. public static class DrawGizmosUtilities
    46. {
    47.     public static void DrawBoxColliderGizmos(BoxCollider box, Color color, bool drawSolid)
    48.     {
    49.         Gizmos.matrix = box.transform.localToWorldMatrix;
    50.         Gizmos.color = color;
    51.  
    52.         Vector3 offset = box.center;
    53.         Vector3 size = box.size;
    54.  
    55.         if (drawSolid)
    56.         {
    57.             Gizmos.DrawCube(offset, size);
    58.         }
    59.         else
    60.         {
    61.             Gizmos.DrawWireCube(offset, size);
    62.         }
    63.  
    64.         Gizmos.matrix = Matrix4x4.identity;
    65.     }
    66. }
    Definitely can be expanded upon (which I will be doing in due time). I just used EditorPrefs to store the value (handled by
    LBGEditorPreferences
    )
     
    zt3ff3n and Reedex like this.