在咱們做項(xiàng)目的時(shí)候經(jīng)常會(huì)遇到要換字體的情況售淡,比如美工覺(jué)著字體不好看或者要做其它語(yǔ)言版本……遇到這種情況………如果UI很多,顯示的字體特別多,那就呵呵了………累不累先放一邊盛末,萬(wàn)一漏了可是一件麻煩的事情…………
需要的朋友可以閱讀腳本,當(dāng)然否淤,直接復(fù)制到你的工程新建C#腳本悄但,放到Endit文件夾直接也可以使用的。
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
/// <summary>
/// 批量修改UI字體腳本石抡,腳本位于Endit文件夾
/// </summary>
public class ChangeFontWindow : EditorWindow
{
//window菜單下
[MenuItem("Window/Change Font")]
private static void ShowWindow()
{
ChangeFontWindow cw = EditorWindow.GetWindow<ChangeFontWindow>(true, "Window/Change Font");
}
//默認(rèn)字體
Font toFont = new Font("Arial");
//切換到的字體
static Font toChangeFont;
//字體類型
FontStyle toFontStyle;
//切換到的字體類型
static FontStyle toChangeFontStyle;
private void OnGUI()
{
GUILayout.Space(10);
GUILayout.Label("目標(biāo)字體:");
toFont = (Font) EditorGUILayout.ObjectField(toFont, typeof (Font), true, GUILayout.MinWidth(100f));
toChangeFont = toFont;
GUILayout.Space(10);
GUILayout.Label("字體類型:");
toFontStyle = (FontStyle) EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f));
toChangeFontStyle = toFontStyle;
if (GUILayout.Button("確認(rèn)修改"))
{
Change();
}
}
public static void Change()
{
//獲取所有UILabel組件
if (Selection.objects == null || Selection.objects.Length == 0) return;
Object[] labels = Selection.GetFiltered(typeof (Text), SelectionMode.Deep);
foreach (Object item in labels)
{
Text label = (Text) item;
label.font = toChangeFont;
label.fontStyle = toChangeFontStyle;
// 如果是NGUI將Text換成UILabel就可以
// UILabel label = (UILabel)item;
// label.trueTypeFont = toChangeFont;
EditorUtility.SetDirty(item); //重要
}
}
}