您现在的位置是:首页 >技术交流 >Unity创建文本时自动修改默认字体网站首页技术交流
Unity创建文本时自动修改默认字体
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class UnityUIEvent
{
private static Font font;
[InitializeOnLoadMethod]
private static void Init()
{
if (font == null)
{
font = AssetDatabase.LoadAssetAtPath("Assets/Resources/Font/Alibaba_PuHuiTi.ttf", typeof(Font)) as Font;
}
EditorApplication.hierarchyChanged += delegate ()
{
ChangeDefaultFont();
};
}
private static void ChangeDefaultFont()
{
if (Selection.activeGameObject != null)
{
if (Selection.activeGameObject != null)
{
Text text = Selection.activeGameObject.GetComponent<Text>();
if (text != null && text.font != null && text.font.name == "Arial")
{
text.font = font;
text.fontSize = 21;
}
}
}
}
}