Unity3d批量更换UGUI-NGUI字体工具

在咱们做项目的时候经常会遇到要换字体的情况,比如美工觉着字体不好看或者要做其它语言版本……遇到这种情况………如果UI很多,显示的字体特别多,那就呵呵了………累不累先放一边,万一漏了可是一件麻烦的事情…………
需要的朋友可以阅读脚本,当然,直接复制到你的工程新建C#脚本,放到Endit文件夹直接也可以使用的。


img_34c8d39ab251f284b1ecc67d153b8d06.png
修改窗口

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

    //默认字体
    Font toFont = new Font("Arial");
    //切换到的字体
    static Font toChangeFont;
    //字体类型
    FontStyle toFontStyle;
    //切换到的字体类型
    static FontStyle toChangeFontStyle;

    private void OnGUI()
    {
        GUILayout.Space(10);
        GUILayout.Label("目标字体:");
        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("确认修改"))
        {
            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); //重要
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值