GUIStyle - Text Alignment

Hi

This is a fairly basic question. I done a quick search but had no luck - Apologies if this has already been answered.

I am trying to center align some text in a Label:

	private void OnGUI()
	{	
		GUIStyle style = new GUIStyle();
		style.alignment = TextAnchor.MiddleCenter;
		GUI.Label(new Rect(Screen.width / 2 - 200,Screen.height - 30,400,30), "Some Text in a Label!", style);
}

It just doesn’t display anything. If I take the GUIStyle param out the text displays fine, although not center aligned.

Thanks in advance.

JT

2 Likes

Most likely the reason is because your GUIStyle is empty, so it displays something “empty”.

If you have a GUISkin assigned, you could start with something like (untested forum-code, may be incorrect, I’m spoiled by Intellisense :wink: ):

GUIStyle style = new GUIStyle(GUI.skin.label);

… that (or something along those lines) should work :wink:

Sunny regards,
Jashan

1 Like

Yeah I did wonder about that, just wasn’t sure how to fix it.

That worked a treat anyway!

Thanks

JT

1 Like

Just for completeness…

I found that ‘exposing’ a skin and dragging it from the Unity editor a great option…

public GUISkin aSkin;

// then later
GUI.skin = aSkin;
GUIStyle style = new GUIStyle(GUI.skin.GetStyle("label"));

A big thanks to shaun for the examples and the GUI skins to play around with: http://forum.unity3d.com/viewtopic.php?t=7713

Ta

JT

1 Like