How to change Font type in Unity?

淺唱寂寞╮ 提交于 2020-01-05 18:53:09

问题


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 + "";
    }
}

回答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.



来源:https://stackoverflow.com/questions/25374384/how-to-change-font-type-in-unity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!