1

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 + "";
    }
}
Jay Kazama
  • 3,167
  • 2
  • 19
  • 25
Savish Khan
  • 53
  • 1
  • 1
  • 8

1 Answers1

1

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.

col000r
  • 609
  • 1
  • 7
  • 14
  • The above code assumes that you will manually assign **UILabel label**. – Rudolfwm Aug 19 '14 at 18:26
  • That is correct. As far as I know there is no way to get a list of available Fonts at runtime. You can however get the default built-in Arial font doing the following: label.trueTypeFont = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; – col000r Aug 20 '14 at 07:20
  • Oh thanks: Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") Did the trick – Savish Khan Aug 21 '14 at 19:05