Actual Rect for GUILayout.Scrollview?

Hello,

I need the resulting Rect for a scrollview I have drawn with GUILayout.

e.g.

scroller = GUILayout.BeginScrollView( scroller );
  GUILayout.BeginVertical();
     // dynamic run-time data drawn
     // in various Verticals/Horizontals
     // possibly with FlexibleSpace
  GUILayout.EndVertical();
GUILayout.EndScrollView();

Rect scrollArea = ?

Or even:

GUILayout.BeginVertical( "Box" );
  // various controls
GUILayout.EndVertical();

Rect boxArea = ?

I know you can use GUILayout.GetRect(…) to “reserve” the space for a single control like a Button–or more specifically for 1 piece of GUIContent–and then call the GUI equivalent with that Rect. But how do you find out how big your scrollview, or styled-BeginHorizontal() is? Even if I rewrite everything to GetRect() and GUI call, I still can’t just union them all together because I’ll need to clip it against the scroll view size, and I don’t think you can get the size of the FlexibleSpace() either.

In case it matters, I need the Rect so I can test dropping items with drag and drop against the screen-space mouse point coords. That works fine already everywhere else, but I at least need a Rect.

Thanks in advance!

As I understand it, you need the size of the clipping rect of the scroll view - not the internal area, right? If this is the case, you can receive the rect like this:

scroller = GUILayout.BeginScrollView( scroller ); 
  GUILayout.BeginVertical(); 
     // dynamic run-time data drawn 
     // in various Verticals/Horizontals 
     // possibly with FlexibleSpace 
  GUILayout.EndVertical(); 
GUILayout.EndScrollView(); 

Rect scrollArea = GUILayoutUtility.GetLastRect ();
1 Like

I have never had any luck with that, and when I was writing this yesterday I noticed it is not in the docs, so I assumed it was broken and had been discretely removed.

I’ll give it a try and let you know.

Thanks!

[Update: Yep, that seems to work perfectly. Now if I can just manage to not obsess over why it didn’t work before, I can try to remember all the things I worked around when it didn’t :slight_smile: That will be so useful–thanks!]

The fact that its missing from the docs is a bug and its being corrected for the next version of Unity.

And is there any function to get the INTERNAL area rect of scrollview?
That is needed in my project to resize window dynamically when new text appears in it…

actually, it is - to use BeginRender and EndRender in the child windows functions.

	protected Rect lastRect;

	public void BeginRender()
	{
		scrollPos = GUILayout.BeginScrollView(scrollPos);
		GUILayout.BeginVertical();
	}

	public void EndRender()
	{
		GUILayout.Label(GUIContent.none, GUIStyle.none, GUILayout.MaxHeight(0), GUILayout.ExpandWidth(true));
		if (Event.current.type == EventType.repaint)
		{
			Rect r = GUILayoutUtility.GetLastRect();
			lastRect.x = lastRect.y = 0;
			lastRect.width = r.width;
			lastRect.height = r.yMin;
		}
		GUILayout.EndVertical();
		GUILayout.EndScrollView();
	}