Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the font size of text in Unity?

How can I make the size of the font in a label larger?

I used this function to display the text :

function OnGUI()
{
    GUI.color = Color.green;
    GUI.Label(Rect(500,350,200,50),"Lose");
}

And that results in:

How can I make this text bigger?

like image 415
Akari Avatar asked Oct 02 '13 11:10

Akari


People also ask

How do you change font in script Unity?

//Create a new Text GameObject by going to Create>UI>Text in the Editor. Attach this script to the Text GameObject. Then, choose or click and drag your own font into the Font section in the Inspector window.

How do you write text in Unity?

To insert a Text UI element in Unity, right-click on the Scene Hierarchy, then select GameObject -> UI -> Text. There are many properties of the Text element. In which Text Field is the most important property. You can type out what you want the text box to show in that field.


2 Answers

Simply create an appropriate GUIStyle and set the fontSize. Pass this to your label and you're good to go.

So something like this:

using UnityEngine;
using System.Collections;

public class FontSizeExample : MonoBehaviour 
{

    GUIStyle smallFont;
    GUIStyle largeFont;

    void Start () 
    {
        smallFont = new GUIStyle();
        largeFont = new GUIStyle();

        smallFont.fontSize = 10;
        largeFont.fontSize = 32;
    }

    void OnGUI()
    {
        GUI.Label(new Rect(100, 100, 300, 50), "SMALL HELLO WORLD", smallFont);
        GUI.Label(new Rect(100, 200, 300, 50), "LARGE HELLO WORLD", largeFont);
    }
}

will result in

like image 80
Bart Avatar answered Oct 19 '22 12:10

Bart


Unity's GUI supports "rich text" tags now.

http://docs.unity3d.com/Documentation/Manual/StyledText.html

So this would work:

GUI.Label(Rect(500,350,200,50),"<color=green><size=40>Lose</size></color>");
like image 16
Calvin Avatar answered Oct 19 '22 14:10

Calvin