Search code examples
c#unity-game-enginengui

How to change Font type in Unity?


How do I program it so that I can change the font type to: Coalition or Arial...

Here is my current code...

using UnityEngine;
using System.Collections;

public class InvSlotHandler : MonoBehaviour {

    private int excess = 1;
    UILabel lbl;

    void Start() {

        GameObject label = new GameObject();
        lbl = label.AddComponent<UILabel>();

        label.transform.name = "#QTY";
        lbl.fontStyle = FontStyle.Normal;
        lbl.fontSize = 15;
        lbl.alignment = NGUIText.Alignment.Right;

        NGUITools.AddChild (gameObject.transform.gameObject, label.gameObject);
    }

    void FixedUpdate() {
        lbl.text = gameObject.transform.childCount - excess + "";
    }
}

Solution

  • Here is an example of how to change the font of a UILabel that uses a dynamic font in NGUI.

    The label shows some text in the original font for 2 seconds, then switches to the other font (the one you assign to otherFont in the inspector)

    using UnityEngine;
    using System.Collections;
    
    public class ChangeFont : MonoBehaviour {
    
        public UILabel label; 
        public Font otherFont;
    
        IEnumerator Start() {
            label.text = "This is a bit of text"; //show text
            yield return new WaitForSeconds(2f); //wait 2 seconds
            label.trueTypeFont = otherFont; //change font
        }
    
    }
    

    If your label was set to use a bitmap font, you'd assign a UIFont to label.bitmapFont instead.