Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Editor Scripts OnGUI()

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

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    156
    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,011
    yes, that's correct, OnGui is ran every frame.
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    1,761
    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. NA-RA-KU

    NA-RA-KU

    Joined:
    May 5, 2013
    Posts:
    919
    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.