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

GetLastRect, how to know?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Rodolfo-Rubens, Nov 4, 2016.

  1. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    Why the code below prints (a.k.a. gets) different values even though the vertical area has a constant size and position:
    Code (csharp):
    1. Rect lastRect = new Rect(); // :P
    2. Event e = Event.current;
    3. //...
    4. GUILayout.EndVertical();
    5. if(e.type == EventType.Repaint) lastRect = GUILayoutUtility.GetLastRect();
    6. Debug.Log(lastRect);
    7. //...
    How do I use GetLastRect properly?
     
    Last edited: Nov 4, 2016
  2. mrdsmt

    mrdsmt

    Joined:
    Jul 28, 2012
    Posts:
    21
  3. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    I think you didn't read my code very well, I'm just declaring the "lastRect" Rect so I can use the values retrieved from GetLastRect in later instructions.

    Yes, I read this a thousand times but still, using get last rect after EndVertical still returns inconsistent/wrong values.

    Thanks for replying.
     
  4. mrdsmt

    mrdsmt

    Joined:
    Jul 28, 2012
    Posts:
    21
    Just write your full example, about the "rect" that you want to get, and show how it's wrong. Your code does not show anything about that, so no one can understand :)
     
  5. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    My code is quite big. My question is about the line 5 in the snippet I posted above.
    I'm calling GetLastRect() right after a GUILayout.EndVertical(), which supposedly gets the rect of the Vertical group, but sometimes it gets a totally wrong value.
     
  6. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    did you try moving
    Code (csharp):
    1.  
    2. Rect lastRect =newRect(); // :P
    3.  
    outside of the OnGUI()... globally?
     
    Rodolfo-Rubens likes this.
  7. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    Dude, you nailed, the only thing I also did was declaring one rect for each use case instead of declaring only one and reusing it!! Thanks a lot!