- Home /
How to set the font size in GUI.Label
Hey guys,
I was just wondering, I have created a GUI Label, but I was wondering, how do you change the font size????
Here is my code:
var font : Font;
function OnGUI () {
GUI.skin.label.font = font;
GUI.Label (Rect (650, 650, 300, 50), "MY TEXT");
}
Thanks
-Grady
Answer by SilverTabby · Jun 29, 2011 at 06:14 AM
Pass a GUIStyle into the function and set the font size inside the GUIStyle.
http://unity3d.com/support/documentation/ScriptReference/GUIStyle.html
so your new code would be
var style : GUIStyle; function OnGUI() { GUI.Label(Rect(650, 650, 300, 50), "HELLO WORLD", style); }
The 3d platformer tutorial has a section on GUIStyles at page 58
http://unity3d.com/support/resources/tutorials/3d-platform-game
As someone who is new, it would be really useful if you, or someone else, could mention how to accomplish this purely through code. I would prefer not to always specify these things from the editor.
@graham_branch First you want to create a GUIStyle variable. Then you will want to change the fontSize of that variable. For C#, it might look something like this:
private GUIStyle guiStyle = new GUIStyle(); //create a new variable
private void OnGUI ()
{
guiStyle.fontSize = 20; //change the font size
GUILayout.Label("Write your text here.", guiStyle);
}
Thanks Steven, instantiating the style was what I was struggling to figure out, though I did figure it out eventually.
Answer by johnnieZombie · Oct 24, 2012 at 07:49 PM
Does this not work for Android?
You probably need to multiply with dpi as fontSize is in pixels
Your answer
Follow this Question
Related Questions
multi line label with different font? 1 Answer
Changing a GUILabel text SIZE 3 Answers
Changing the size of a GUI label 1 Answer
Why does my non-dynamic label text not resize in Android? 2 Answers