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

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();
}

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,