Unity3d 开发技术Unity3D游戏开发Unity教程合集

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

2016-07-17  本文已影响1740人  好怕怕

在咱们做项目的时候经常会遇到要换字体的情况,比如美工觉着字体不好看或者要做其它语言版本……遇到这种情况………如果UI很多,显示的字体特别多,那就呵呵了………累不累先放一边,万一漏了可是一件麻烦的事情…………
需要的朋友可以阅读脚本,当然,直接复制到你的工程新建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");
    }

    //默认字体
    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); //重要
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读