Center GUILayout.Label

Hi Guys, i have a GuiLayout.Button and a GuiLayout.Label in the same line… The Button must be at the left, the label must be at the center…
I have this:

           GUILayout.BeginHorizontal ("box");     
            if (GUILayout.Button ("Indietro", GUILayout.Width (80))) {
                main_menu ();
            }
            GUILayout.Label ("Gsfea Yhsa");        
            GUILayout.EndHorizontal ();

The String: “Gsfea Yhsa”
must be at the center!! How can i do?

Use GUILayout.FlexibleSpace:

GUILayout.BeginHorizontal ("box");     

if (GUILayout.Button ("Indietro", GUILayout.Width (80))) {
	main_menu ();
}

GUILayout.FlexibleSpace();
GUILayout.Label ("Gsfea Yhsa");
GUILayout.FlexibleSpace();

GUILayout.EndHorizontal ();
3 Likes

I tried, but with flexiblespace the string isn’t at the center of window… With flexiblespace the string is at the center between the end of Gui.Button and the end of gui.window…
The string must be at the center of the gui.window! Under the string “Compra merce”

Oh I see what you mean, well it’s pretty simple to workaround just split it into 2 layout areas (one for the button and one for the label) and use FlexibleSpace in the area with the label.

PS: the label’s area should have the “box” and be created before the button’s in the code. You probably need to tweak the rect x/y position of the button’s area to be aligned to the label. Of course you can also act the reverse way (that is button’s is “box” and tweak the label’s rect)

Ok thank you :)…
I resolved:

               GUILayout.BeginHorizontal ("box");
		
		GUILayout.BeginArea(new Rect(16,24,80,30));
		if (GUILayout.Button ("Indietro", GUILayout.Width (80))) {
			selectionGridInt = -1;
			main_menu ();
		}
		GUILayout.EndArea();
		GUILayout.FlexibleSpace();
		GUILayout.Label (fornitori [x].getNome ());	
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal ();

I inserted the rect dimension in BeginArea manually…
Is there a way to obtain the GUILayout.BeginHorizontal (“box”) rect and insert it into BeginArea?
Instead to insert manually the rect dimension into BeginArea

To you use .Layout in center, you make a rect:

Rect(Screen.width/2,Screen.heigth/2,80,30);

(I am aware that I am responding to a 8 years old message)
I think you misunderstood the question. This is for Editor UI. Custom menus. Therefore “Screen.” is irrelevant here.