Search Unity

Can Anyone Confirm - Unity calling OnGUI twice per frame?

Discussion in 'Scripting' started by NYGhost, Dec 4, 2007.

  1. NYGhost

    NYGhost

    Joined:
    Sep 20, 2007
    Posts:
    55
    why would Unity call OnGUI twice pwe frame?

    Code (csharp):
    1.  
    2. function Update() {
    3.     print("Update " + Time.frameCount);
    4. }
    5.  
    6. function LateUpdate() {
    7.     print("LateUpdate " + Time.frameCount);
    8. }
    9.  
    10.  
    11. function OnGUI () {
    12.      print("OnGUI " + Time.frameCount);
    13. }
    14.  
     
  2. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    OnGUI is called at least twice per frame. First to compute the layout and then to actually draw the GUI. Try to do a
    Code (csharp):
    1. Debug.Log(Time.frameCount + ": " + Event.current);
    To get a better understanding of the call structure.
     
  3. NYGhost

    NYGhost

    Joined:
    Sep 20, 2007
    Posts:
    55
    thanks.
    perhaps that detail should be added to the documentation? :wink:
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    OnGUI can be called many times per frame. The GUI system does different things when OnGUI gets called - first it does a prepare pass, recording what components are there (so tab order can be figured out, GUILayout positions are calculated, etc). Then it does the actual event processing; MouseDown, MouseUp, KeyDown, KeyUp, Repaint.

    to see what it's actually doing, take a look at Event.current.type
     
  5. NYGhost

    NYGhost

    Joined:
    Sep 20, 2007
    Posts:
    55
    I came across this because I was trying to figure when the mouse was over a GUI component so I could ignore mouse events on the 3d objects in the background.

    I could not find anything that would give me that, the closest was GUIUtility.hotControl.

    I had to store the rects of the gui areas to test when the user click with Rect.Contains() to get it to work.

    it would be useful if GUIUtility had a flag that would be set when the mouse over a gui element somenthing like GUIUtility.mouseOver or something like that.
     
  6. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    That is solved in the next Unity release