Possible to create a box with GUILayout?

I am trying to make a box around some controls, kind of like a groupbox in .Net.

GUILayout.Box("Movement");
	GUILayout.BeginVertical();
		GUILayout.BeginHorizontal();
			if (GUILayout.RepeatButton ( GUIContent( "", iconRotateLeft, "Turn Left"), GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth) )) {
				GetComponent(FPSGUIMouseWalk).EnableAction("TurnLeft");
			}
			if (GUILayout.RepeatButton ( GUIContent( "", iconUp, "Forward"), GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth)  )) {
				GetComponent(FPSGUIMouseWalk).EnableAction("Forward");
			}
			if (GUILayout.RepeatButton ( GUIContent( "", iconRotateRight, "Turn Right"), GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth)  )) {
				GetComponent(FPSGUIMouseWalk).EnableAction("TurnRight");
			}
		GUILayout.EndHorizontal();
		GUILayout.BeginHorizontal();
			if (GUILayout.RepeatButton ( GUIContent( "", iconLeft, "Move Left"), GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth)  )) {
				GetComponent(FPSGUIMouseWalk).EnableAction("Left");
			}
			if (GUILayout.RepeatButton ( GUIContent( "", iconDown, "Backward"), GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth)  )) {
				GetComponent(FPSGUIMouseWalk).EnableAction("Backward");
			}
			if (GUILayout.RepeatButton ( GUIContent( "", iconRight, "Move Right"), GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth)  )) {
				GetComponent(FPSGUIMouseWalk).EnableAction("Right");
			}
		GUILayout.EndHorizontal();
	GUILayout.EndVertical();

The buttons arrange nicely in 2 rows of 3 buttons per row, but I can’t figure out how to get a box to automatically size itself to encompass the buttons.

The manual for GUILayout.Box states:

What does that mean?

Change

GUILayout.Box("Movement");
  GUILayout.BeginVertical();
    ...
  GUILayout.EndVerical()

to

GUILayout.BeginVertical("Movement");
  ...
GUILayout.EndVerical()

GUILayout.Box simply gives you a solid box, it’s not meant to encompass other controls.

I prefer to use GUI “groups” for something like this:

Thanks for the help.

I changed the code to:

GUILayout.BeginVertical("Box");

That placed a box around the controls. Padding is a bit strange, but I should be able to fix that with some tweaking.

1 Like