• 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
0
Question by Befall · May 31, 2012 at 11:24 PM · guilabelcenter

Center GUI.Label similar to GUIText?

The only examples I could find of "centering" GUI Labels here have been the text within the labels. However, I want to physically center the text as you can do with GUITexts.

For example, I have a GUI Label that's 200 wide with the text "Hello". If I set the x-coor to Screen.width/2, I want to be able to have that text always centered by anchoring the text by the middle to that coordinate. You can do the same thing with a GUIText using TextAnchors, but they function differently. Labels simply center the text within their Rect.

Is there any way to physically anchor a GUI.Label?

Comment
Add comment · Show 2
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 Chimera3D · May 31, 2012 at 11:27 PM 0
Share

I don't think so, because it's not a gameobject. But there are some ways you can center it in the editor, if that's what you're asking for.

avatar image Befall · May 31, 2012 at 11:30 PM 0
Share

Nah, it's a dynamic GUI system that adjusts based on window size, so I'd need a way to always keep it center on one code-deter$$anonymous$$ed spot. $$anonymous$$ight just have to figure out a system of centering by changing the label width and font size accordingly. Thanks for the input.

2 Replies

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

Answer by GutoThomas · May 31, 2012 at 11:56 PM

You can multiply the number of letters in your string by a constant value which would be the pixels by letter value and then place it in Screen.widht/2 - (number of letters * constant)/2.

 string myString = "hello";
 textWidth = myString.Length * 10;

 //10 is a constant which will obviously vary as you change the font or its size, 
 //so you'll need a little of pacience to get a good value there
 
 GUI.Label(new Rect(Screen.width/2 - textWidth/2, Screen.height/2, textWidth, 20), myString);

This is not the better way to do it but can give you some good results. I think other way of doing it is creating a custom GUISkin and play around with the Label options. May exist some sort of option for centering the text.

Comment
Add comment · Show 2 · 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 Bunny83 · Jun 01, 2012 at 01:25 AM 0
Share

This isn't really straight forward ;) magic numbers are always a bad choice. You could at least use GUIStyle.CalcSize to get the size of the text in pixels.

avatar image Befall · Jun 01, 2012 at 01:25 AM 0
Share

This would work if I wasn't using dynamic text, but as I am, I'm likely going to simply increase the size of the label as I increase the font size. Thanks for the input!

avatar image
1

Answer by Bunny83 · Jun 01, 2012 at 01:22 AM

Just take a look at GUIStyles and especially GUIStyle.alignment ( TextAnchor)

If you don't want to create your own GUISkin, which would simplify such setups a lot since you can easily edit your skin in the inspector, You can create a temporary GUIStyle like this:

 //C#
 void OnGUI()
 {
     // copy the "label" style from the current skin
     GUIStyle centeredTextStyle = new GUIStyle("label");
     centeredTextStyle.alignment = TextAnchor.MiddleCenter;
     GUI.Label(yourRect, "The text",centeredTextStyle);
 }

If you use a GUISkin you can either change the label style (not recommended) which will influence all labels that are drawn with this skin, or add a custom style to the skin. Just name your style e.g. "centeredText" and use it like this:

 //C#
 public GUISkin skin; // assigned in the inspector
 void OnGUI()
 {
     GUI.skin = skin;
     // Just use the style name you like.
     GUI.Label(yourRect, "The text", "centeredText"); 
 }

Just as an additional hint: everything in the UnityGUI is drawn using styles. You can use any style to draw any content.

This will draw a label that looks like a button:

 GUI.Label(rect, "MyText", "button");

This will create a button that is of course clickable but looks like a label:

 if (GUI.Button(rect, "MyText", "label"))
 {
 }

Feel free to explore the power of the GUI system ;)

Comment
Add comment · Show 2 · 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 Befall · Jun 01, 2012 at 01:27 AM 1
Share

Did you read my question? The GUIStyle.TextAnchor is pointless for what I need, that centers the text WITHIN the label, but does not center the label upon its location. I'll look into skins, but Noob-E's comment basically answered my question. Thanks.

avatar image Bunny83 · Jun 01, 2012 at 01:40 AM 0
Share

Ehmm, yes i read the question and why exactly is TextAnchor pointless? You usually create your label with the maximum size the text should be allowed to be displayed. The rect of an GUI element defines it's clipping rectangle. When you positioning your Label at

 Rect(Screen.width/2 - 100, Screen.height/2 - 10, 200, 20)

The text will be centered when you set the alignment to center.

If you don't even want to clip your text, just do it like this:

 Rect(0, Screen.height/2 - 10, Screen.width, 20);
 // or even
 Rect(0, 0, Screen.width, Screen.height);

With $$anonymous$$iddleCenter it will be exactly at the screen's center.

You can also use GUILayout. GUILayout elements are autopositioned.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

GUI's documentation lacks of examples 1 Answer

How to make a GUI Label always in the center 1 Answer

How Do I Center A GUI Label? 5 Answers

Text Alignment 2 Answers

Problem Trying To Get GUI.Label Centred 2 Answers


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