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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Gizmo drawing outside OnDrawGizmos, why not allowed?

Discussion in 'Scripting' started by ShuuGames, Oct 18, 2013.

  1. ShuuGames

    ShuuGames

    Joined:
    Jun 25, 2013
    Posts:
    188
    Hi,
    In Unity there are two classes Gizmos and Handles that can both do some drawing in scene view. Why is it so that Gizmos.* can only be used in OnDrawGizmos? Why are there restrictions in place saying what I must draw in OnSceneGUI and in OnDrawGizmos?

    In particular I want to draw "wire sphere" and "disk" to visualize something complex in scene view when it's selected, but I can't do it in OnGrawGizmos (there's no disk drawing) neither in OnSceneGUI (there's no wire sphere).
     
  2. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    same question, why not or what the trick here? Want to draw WireSphere in OnSceneGUI
     
  3. Nymisu

    Nymisu

    Joined:
    Feb 17, 2015
    Posts:
    13
    I'm answering this in case someone stumbles to this and is exceptionally confused on why this is:
    Because Gizmos are inherited from Monobehavior, and OnSceneGUI scripts inherit from Editor.
    This is required first and foremost to make the editor and the game separate entities, along with the performance benefits.

    You can have a gizmo drawing script which you call with the custom inspector to toggle its gizmos on and off with a public bool, however.
    Don't forget that Handles underneath OnSceneGui does have some draw functions aswell.
     
  4. Fahrettin52

    Fahrettin52

    Joined:
    Apr 7, 2014
    Posts:
    1
    private int radius = 25; //
    private GameObject object;

    public void OnDrawGizmos() {
    Gizmos.color = Color.red;
    Gizmos.DrawSphere(object.transform.position, radius);
    }

    upload_2017-8-3_15-42-22.png

    Don't forget to select this.
     
    devxnuage likes this.