- Home /
The question is answered, right answer was accepted
Text Alignment
is it possible to center align the output of this statement? I keep running into a syntax error of some sort. Any help would be greatly appreciated!
GUI.Label (Rect (45, 45, 100, 40), GUI.tooltip);
Answer by jahroy · May 26, 2011 at 12:30 AM
Alignment can be controlled using GUIStyles:
var tooltipStyle : GUIStyle;
function OnGUI ()
{
GUI.Label(Rect(0, 0, 100, 100), GUI.tooltip, tooltipStyle);
}
You can then create a GUIStyle, drag it into the inspector, and change its alignment property.
You don't have to create one -- declaring a GUIStyle (that first line) should set it up in the inspector for you.
Hello Owen, I added the GUIStyle Variable at the top and edited the alignment in the Inspector tab but when i press play it still shows up in the Upper Left. :(
Answer by GlennHeckman · May 26, 2011 at 03:12 AM
You can create a GUIStyle by simply declaring a variable at the top of your script which when the script (component) is selected, you can go to the Properties panel and modify the style that way. Unity3d Docs GUIStyle
var myStyle:GUIStyle;
You can also create a GUISkin and modify the values through the skin as well. Unity3d Docs GUISkin
OR .. you can change the alignment on the individual reference of your label (or button, etc ..) directly through the code below. Be aware though, that once you change the Alignment property, it stays changed for EVERY Other label (or button, etc ..) reference. The way to change just one item would be to follow the code below: (change style, create GUI element, change style back to original state)
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
GUI.Label(Rect(10,10,200,400),"My Text");
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
Below are the possible Text Alignment options. If you want to change the alignment for a button instead of a label, just change the word "label" for whatever default style it is that you want to change.
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
GUI.skin.label.alignment = TextAnchor.MiddleRight;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.skin.label.alignment = TextAnchor.LowerCenter
GUI.skin.label.alignment = TextAnchor.LowerLeft
GUI.skin.label.alignment = TextAnchor.LowerRight
GUI.skin.label.alignment = TextAnchor.UpperCenter
GUI.skin.label.alignment = TextAnchor.UpperLeft
GUI.skin.label.alignment = TextAnchor.UpperRight
Follow this Question
Related Questions
Center GUI.Label similar to GUIText? 2 Answers
GUI's documentation lacks of examples 1 Answer
Trouble using C# tooltips 1 Answer
How to make a GUI Label always in the center 1 Answer
How Do I Center A GUI Label? 5 Answers