- Home /
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?
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.
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.
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.
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.
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!
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 ;)
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.
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
Follow this Question
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