- Home /
Encase GUILayout area
Hi, I need to do this: 1)create a nice box with a title on top 2)add an unknown number of buttons in this box I can't figure out how to do this because the box and the area require an explicit size, but I can't know this values a priori
Answer by Molix · Aug 08, 2010 at 07:12 PM
As you've probably noticed, the GUILayout functions don't take Rect's. However, an important one does: BeginArea.
So basically you define a rectangle with BeginArea, then layout your buttons inside without worrying about their coordinates. Depending on how many buttons and the size of your area, you may want to put the buttons in a scroll view. e.g.
public string[] buttonNames; private Vector2 scroller = new Vector2();
void OnGUI() { GUILayout.BeginArea( new Rect( 50, 50, 100, 200 ) ); scroller = GUILayout.BeginScrollview( scroller ); foreach( string buttonName in buttonNames ) { if( GUILayout.Button( buttonName ) ) { Debug.Log("You pressed " + buttonName ); } } GUILayout.EndScrollview(); GUILayout.EndArea(); }
thank you but this is not what i really want. basically i need to make something like this image (http://unity3d.com/support/documentation/Images/manual/gui-Layout-0.jpg) but without an hardcoded size. no matter how many buttons i place, the box must always resize to surround them
I misunderstood the "box and the area require an explicit size" as being your requirement. In that case, just change the BeginArea/EndArea to BeginVertical with a style, e.g. BeginVertical( GUI.skin.GetStyle("Box") );
Answer by Uzquiano · Mar 16, 2011 at 01:24 PM
Hei Patrik this answer comes a bit late though for the future some will read it, and it will help anyway.
So, this is my solution for your same problem:
GUILayout.BeginArea(Rect(810,120,180,700));
// Starts a vertical group
GUILayout.BeginVertical ("box", GUILayout.ExpandHeight(false));
First create a Rect long enough that it will include as many elements as needed and then in the BeginVertical add the ExpandHeight control as it is shown in the previous code.
Cheers,
Your answer
Follow this Question
Related Questions
GUILayout margin bug? 2 Answers
Text color change onHover 2 Answers
Drawing a GUI box by specifying the two corners 1 Answer
Strange behaviour GUI.Box 1 Answer