• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
10
Question by RSunity · Dec 08, 2010 at 02:02 AM · guigamescreenlabelcenter

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brenden-Frank · Dec 10, 2012 at 09:27 PM 1
Share

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.

avatar image sona.viswam · Jan 10, 2013 at 09:49 AM 0
Share

Useful code :)

avatar image Poulpc · Apr 24, 2013 at 10:56 AM 0
Share

thx just what i needed

avatar image herufeanor · Aug 24, 2013 at 07:15 AM 4
Share

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);
avatar image danielng01 · Oct 10, 2013 at 01:58 PM 0
Share

Useful : )

Show more comments
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesus_Freak · Dec 08, 2010 at 02:12 AM 1
Share

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

avatar image Jesus_Freak · Dec 08, 2010 at 02:12 AM 0
Share

i was ai$$anonymous$$g taht at anyone that might see this later besides you, RSunity

avatar image Brenden-Frank · Dec 10, 2012 at 09:28 PM 0
Share

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.

avatar image
3

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");

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem Trying To Get GUI.Label Centred 2 Answers

Center GUI.Label similar to GUIText? 2 Answers

Unity 3d C# GUI box goes off to the side needs to be in the center of the screen 0 Answers

Why isn't the text centered in my game screen? 3 Answers

GUI's documentation lacks of examples 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges