Centre buttons in a GUI window C#

Hello,
I was wondering if anyone could help me with a problem regarding Unity GUI in C#.

I have made a GUI.Window: Image

	void OnGUI()
{
bottompanelRect3 = GUI.Window(3, bottompanelRect3, bottompanelFunc3, "Region")
}

Function:

void bottompanelFunc3(int id)
		{
			GUILayout.BeginHorizontal();

		if (GUILayout.Button (regionImage, GUILayout.Width(40), GUILayout.Height(40)))
			{
			}
		if (GUILayout.Button (capitalImage, GUILayout.Width(40), GUILayout.Height(40)))
			{
			}

			GUILayout.EndHorizontal ();

		}

I would like to align the buttons in the centre of the window and not to the left like they are now. I would also like to know how to align the windows title “Region” to the left as well so that I can experiment with what looks good.

Thanks in advance.

edit: Thanks Patico and Ghidera

Use FlexibleSpace before and after yor buttons. something like this:

void bottompanelFunc3(int id){
   GUILayout.BeginHorizontal();

   GUILayout.FlexibleSpace();

   if (GUILayout.Button (regionImage, GUILayout.Width(40), GUILayout.Height(40))){
   }
   if (GUILayout.Button (capitalImage, GUILayout.Width(40), GUILayout.Height(40))){
   }

   GUILayout.FlexibleSpace();

   GUILayout.EndHorizontal ();
}
1 Like

On a marginally related note, if your GUI is more than a few lines it’ll start getting hard to find specific things due to the lack of indenting. Somewhere on the forums I saw a tip that really makes things easier to manage: Use braces! Although the gui areas/windows etc don’t use them, you can add them in for indentation anyway - like this:

void bottompanelFunc3(int id){
	GUILayout.BeginHorizontal(); {
		GUILayout.BeginVertical(); {
 
			GUILayout.FlexibleSpace();
 
			if (GUILayout.Button (regionImage, GUILayout.Width(40), GUILayout.Height(40))){
			}
			if (GUILayout.Button (capitalImage, GUILayout.Width(40), GUILayout.Height(40))){
			}
 
			GUILayout.FlexibleSpace();
		}
 	}
	GUILayout.EndHorizontal ();
}

I added the vertical just for demonstation purposes.