- Home /
Unity Scrolling Textbox
I'm trying to build a GUI scrolling textbox where the content will be of unknown length and will change (but not user editable).
While BeginScrollView is nice, I am forced to determine the scroll height before I build any content, not that there's any way to poll the height of content or to get a TextArea to auto-adjust its height depending on its content.
Answer by Ray-Pendergraph · Sep 08, 2011 at 11:08 PM
Use GUILayout.BeginScrollView if you can it's a lot less troublesome. If you can't then the CalcXXXX methods on GUIStyle will help you calculate the area needed by rendered text. I think these only work in OnGUI so this encourages you to recalculate every OnGUI (that's bad) or have a 'calculated' flag to signal recalculation is needed in OnGUI when new text or data model content is set.
That doesn't help. I am using BeginScrollView but I have the issue of not knowing how big to make the scrollable area as the text hasn't even been rendered yet.
Exactly, these methods will precalculate how big the controls need to be to accommodate the text when it is finally rendered but before you render them the first time. Then you can size your components manually accordingly. $$anonymous$$aybe I am missing what you are doing.
Ah! I missed that it was a GUILayout, not just the GUI. That has (had?) the issue of not allowing me to place it on the screen in absolute coordinates (or rather, relative to the GUI.Window) but someone pointed me at GUI.Group(), so that solved that.
GUILayout.BeginArea has Rect coords, but think of Area as a large rectangle.
Answer by Draco18s · Sep 09, 2011 at 08:19 PM
This is what I did:
GUI.BeginGroup(Rect(0,395,250,305));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(240), GUILayout.Height(275));
GUILayout.Label(descriptString);
GUILayout.EndScrollView();
GUI.EndGroup();
I wouldnt use GUI combined with GUILayout. If you need to place the Scrollview, use the GUILayout.BeginArea() http://unity3d.com/support/documentation/ScriptReference/GUILayout.BeginArea.html