Search Unity

How to add component to the SceneView Gizmos dropdown without using OnDrawGizmos function

Discussion in 'Immediate Mode GUI (IMGUI)' started by Xarbrough, Oct 12, 2018.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    When I add OnDrawGizmos to my MonoBehaviour, the component is automatically added to the Gizmos dropdown list in the SceneView:

    GizmoWindow.png

    However, I prefer separating my editor from runtime code, which is why I like to use the DrawGizmo attribute. With the attribute, I decorate a static function within my editor class and can draw the same Gizmo as before.

    Sadly, I have noticed, that now my component does not appear in the list anymore. Hence, I cannot toggle the Gizmo via the list or by expanding/collapsing the component in the inspector.

    Is there a way for me to register my custom Gizmo function with the dropdown list?

    A partial solution, to at least toggle gizmos from the inspector, is to use:

    UnityEditorInternal.InternalEditorUtility.GetIsInspectorExpanded(component);


    With the internal utility I can check if the component is expanded and then decide whether to draw or now. However, I would still like it to also appear in the SceneView Gizmos list.
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Doesn't look like it. A quick read of the source says that they gather all the valid classes through an 'AnnotationUtility' class. It seems like DrawGizmo isn't included in the gathering.

    (DrawGizmo also doesn't capture mouse events, unfortunately.)

    EDIT: Since writing this post ages ago, I have since discovered that the DrawGizmo attribute can capture mouse events if you pass in GizmoType.Selectable to it.
     
    Last edited: Mar 5, 2020
  3. TS-Trooper

    TS-Trooper

    Joined:
    Apr 30, 2019
    Posts:
    18
    Not sure if it is still relevant, but for whomever stumbles on this:

    I used the DrawGizmo attribute for drawing and the checkbox in the drop down does not appear.
    But, now I also implemented an empty OnDrawGizmos method in the component. now the checkmark appears and the method with the DrawGizmo attribute is executed only if the checkbox is marked.. So I guess that works.