x


How Do I Center A GUI Label?

Hi, I'm using the following:

GUI.Label (Rect (0,0,100,50), "BLAH");

What would I set it to for it to be centered on the screen instead of the left corner?

more ▼

asked Dec 08 '10 at 02:02 AM

RSunity gravatar image

RSunity
74 8 8 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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);
}
more ▼

answered Dec 08 '10 at 02:26 AM

Eric5h5 gravatar image

Eric5h5
80.2k 41 132 519

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.

Dec 10 '12 at 09:27 PM Brenden Frank

Useful code :)

Jan 10 at 09:49 AM sona.viswam

thx just what i needed

Apr 24 at 10:56 AM Poulpc
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 08 '10 at 02:09 AM

Jesus_Freak gravatar image

Jesus_Freak
1.2k 38 44 59

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

Dec 08 '10 at 02:12 AM Jesus_Freak

i was aiming taht at anyone that might see this later besides you, RSunity

Dec 08 '10 at 02:12 AM Jesus_Freak

I was employing this solution until I started using Matrix4x4 to scale my GUI based on screen resolution. The sizing works, however the offset values don't seem to want to comply.

Dec 10 '12 at 09:28 PM Brenden Frank
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3681
x798
x484
x157
x88

asked: Dec 08 '10 at 02:02 AM

Seen: 12144 times

Last Updated: Apr 24 at 10:56 AM