know GUILayout current screen position

Hi guys.

I’m using GUILayout (not GUI) for GUI rendering, and I’ve pics to print on screen, but they aren’t all at the same resolution, and the available space for printing them is also variable.

Is there a way to know where the current GUILayout printing (Box, Label…) is on screen ?

i.e. to get a result saying that the next thing printed will be at (450px, 375px), so that I can calculate how much pixels I have left on screen’s width/height to resize my pics.
I’ve tried GUILayoutUtility.GetLastRect() but the rect returned doesn’t seem very accurate.

Thanks !

GetLastRect is probably what you want, but a couple things to keep in mind:

  • If you’re in an Area or Window, the coords returned may be relative to that (though the size should be correct).

  • If you’re saving the Rect between frames, remember that OnGUI is run multiple times per frame (once per GUI event), and GetLastRect is not ‘right’ in every event, i.e. if you’re saving the Rect between frames you’ll probably need to wrap it in:

    if( Event.current.type == EventType.Repaint )
    lastRect = GUILayoutUtility.GetLastRect();

If you’re just using the Rect locally then you won’t need that since it will be correct during the events that it needs to be.

(sorry about formatting, lists+code format does not seem to work)

Great thanks !

The event type wasn’t an issue, but my GUILayout was indeed in an aera.

Thanks again :wink: