【问题标题】:How to change Font type in Unity?如何在 Unity 中更改字体类型?
【发布时间】:2014-08-19 01:25:44
【问题描述】:

如何对其进行编程,以便将字体类型更改为:Coalition 或 Arial...

这是我当前的代码...

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

【问题讨论】:

    标签: c# unity3d ngui


    【解决方案1】:

    这是一个示例,说明如何在 NGUI 中更改使用 动态字体UILabel 的字体。

    标签以原始字体显示一些文本 2 秒,然后切换到另一种字体(您在检查器中分配给 otherFont 的字体)

    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
        }
    
    }
    

    如果您的标签设置为使用位图字体,则您需要将 UIFont 分配给 label.bitmapFont

    【讨论】:

    • 以上代码假设您将手动分配UILabel标签
    • 没错。据我所知,没有办法在运行时获取可用字体的列表。但是,您可以通过以下方式获取默认的内置 Arial 字体: label.trueTypeFont = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
    • 哦,谢谢:Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") 成功了
    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多