- Home /
Answer by Eric5h5 · Dec 08, 2010 at 02:26 AM
You need a GUIStyle that has a centered alignment. You can do this programmatically by getting an existing style and setting the alignment:
function OnGUI () {
var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.UpperCenter;
GUI.Label (Rect (Screen.width/2-50, Screen.height/2-25, 100, 50), "BLAH", centeredStyle);
}
Or you can make your own style, set it up in the inspector to include centering, and use that:
var myStyle : GUIStyle;
function OnGUI () { GUI.Label (Rect (Screen.width/2-50, Screen.height/2-25, 100, 50), "BLAH", myStyle); }
Is it just me, or does this solution not seem to work? That alignment property seems to have no bearing on GUI being centered on the screen.
This is the right idea, but there's a problem. Just calling GetStyle() on the skin will return the style by reference, so when you change the alignment you'll be changing the default label alignment for the skin. Ins$$anonymous$$d, you should create a new GUIStyle based on this one. Also, you should use GUI.skin.label, ins$$anonymous$$d of GUI.skin.GetStyle("Label"), as that allows static checking (that is, the compiler can tell you if you type it wrong). So the updated version would be:
var centeredStyle = GUIStyle(GUI.skin.label);
Answer by Jesus_Freak · Dec 08, 2010 at 02:09 AM
hey there again! here's the code to do that:
var windowWidth : int = 200; var windowHeight : int = 200; /*var here from your other script should be Str. doesn't have to be in this script though. that's what ScriptName.StaticVarName does. goes through that script to find that static var. only works for static vars as far as i know. */
function OnGUI()
{
GUI.Window(0, Rect((Screen.width / 2) - (windowWidth / 2),
(Screen.height / 2) - (windowHeight / 2),
windowWidth,
windowHeight), Message.Str); // should be centered.
}
and if it's not centered, play around with the window height / width, and if it's still not working, let me know.
the other var and script i'm talking about is from this person's other question: http://answers.unity3d.com/questions/30742/gui-message-box
i was ai$$anonymous$$g taht at anyone that might see this later besides you, RSunity
I was employing this solution until I started using $$anonymous$$atrix4x4 to scale my GUI based on screen resolution. The sizing works, however the offset values don't seem to want to comply.
Answer by squishy312 · May 13, 2014 at 11:20 AM
This seems to work for me in C#, should be similar in Javascript. :)
GUI.Box (new Rect ((Screen.width)/2 -(Screen.width)/8,(Screen.height)/2-(Screen.height)/8,(Screen.width)/4,(Screen.height)/4), "Centered Box");
Answer by langalang · Feb 24, 2014 at 10:54 PM
Another way is to set the alignment to "Upper Center" in the GUIStyle. Set your label to be anchored to the x=0 and height you want, then make sure your label width is set to the screen width. Eg GUI.Label (Rect (0, Screen.height/2-25, Screen.width, 50), "BLAH", myStyle);
This works with dynamic text.
Answer by shikekaka · Oct 03, 2020 at 10:04 AM
Thanks, everyone!
I spent half a day on this,
and I realize it was because of the Rect not centering,
not because of the GUI Label.
A quick fix would be to move the starting point of the Rect back by half of the of its width, then the GUI Label should be at the center.
float rectWidth = 500;
float rectHeight = 30;
GUI.Label(new Rect(Screen.width / 2 - ( rectWidth / 2 ), Screen.height / 2, rectWidth, rectHeight), "Text", centeredStyle);
centeredStyle is from the answer above by @Eric5h5
Your answer
Follow this Question
Related Questions
Problem Trying To Get GUI.Label Centred 2 Answers
Why do a GUI.label and GUI.textfield not align vertically? 1 Answer
How to clear a GUI Label 1 Answer
Fix Resizing For GUI Label? 1 Answer