Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Need to draw gizmos on every editor frame even if inspector collapsed

Discussion in 'Scripting' started by syscrusher, Jan 26, 2018.

  1. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    I have a MonoBehaviour script for procedural geometry that needs to draw gizmos (rather than just drawing geometry) so it can be selected in the editor. Unity doesn't call the OnDrawGizmos() method if the inspector for the component is collapsed.

    Is there a way to make Unity call OnDrawGizmos() unconditionally for my object? I've found several alternative hooks that allow me to get a callback every frame, but I can't call the Gizmos function from another callback. Just drawing the visual doesn't make it selectable in scene view.

    The DrawGizmo attribute doesn't help, by the way, as it has no effect if the inspector is collapsed, just like OnDrawGizmos() itself.

    Thanks for any suggestions.
     
    Last edited: Jan 26, 2018
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    From my understanding, OnDrawGizmos are a way to create handles that can manipulate data within a MonoBehaviour, and OnDrawGizmosSelected are a way to display (not manipulate) handles related to GameObjects as a whole (such as radius effects). If you're trying to manipulate MonoBehaviour data, you really should have the result of that data be visible (in a non-collapsed inspector)- it makes little sense to make it unconditionally enabled in that respect- you CAN lock the GameObject inspector in that window though and open a second one to browse others simultaneously.

    If you want to manipulate handles and they're not tied to a specific GameObject or MonoBehaviour, such as for a voxel-building kind of scenario, you shouldn't be using OnDrawGizmos at all, you can instead do it in any editor window using OnSceneView and the Handles class. One option is to make your own editor window, give it a MenuItem attribute so you can open it from one of the menus, and then dock it like any other window (like the inspector itself). This will give you the OnSceneView you need to draw handles on the screen, and also a place to put any buttons or information about the operations you're performing with the handles, enable or disable some of them, etc...

    If you truly don't want to deal with any selected/open windows of any kind, you can make your own arbitrary OnSceneView function in any class, then manually add that function to the SceneView.OnSceneGUIDelegate using a static function and the InitializeOnLoad attribute to call add it automatically whenever Unity is opened and force the handles to be displayed without a proper container for it. Instead of InitializeOnLoad, you could also make it so that you have two MenuItems- one for enabling the handles by adding the function to SceneView.OnSceneGUIDelegate, and one for disabling the handles by removing it.

    Does that make sense? This isn't something I've ever done though, so I can't be 100% that any of these solutions are exactly what you're looking for.
     
  3. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    Very much so. Thank you for an extremely detailed and well-thought-out reply. I'll investigate further based on your suggestions. If I could "like" your post more than once, I'd do so. :)