Search Unity

Editor Scripts OnGUI()

Discussion in 'Scripting' started by Homicide, Feb 13, 2018.

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Hi guys. Im curious. Assuming i have something simple such as a custom editor window with a toggle set up to turn on or off a light...

    if im correct, the OnGUI happens with each action, for eg movement of the mouse on screen, etc.

    So i noticed that Debug.Log will tell me with each stroke of the mouse over and over "lights off" after i toggle 'turn off' the lights, and i got to thinking, is it really running a routine each and every single frame OnGUI does something? Attempting to turn off all the lights again and again.... it just doesnt seem optimal, so i assume im doing something completely wrong.

    thanks, bit newish here.
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    yes, that's correct, OnGui is ran every frame.
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    It's run every frame, but can be multiple times per frame as it is run for every input event that happened that frame too.
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Yeah, it will assign fields every frame.
    If you are using a public property to call a function, you should check whether the value has changed first.