Unity Editor 替换UILabel 引用的Font

作者:雨辰 发布于:2017-10-17 19:43 Tuesday 分类:Unity3D

using UnityEngine;
using UnityEditor;

/// <summary>
/// 根据鼠标点中的对象批量修改所有UI字体脚本,脚本位于Editor文件夹
/// </summary>
public class ChangeFontWindow : EditorWindow
{
    //是否改变当前字体
    private static bool isChangFont = false;
    //是否改变引用字体库
    private static bool isChangFontLibrary = false;

    //是否查找字体引用
    private static bool isSelectFont = false;

    private static Font selectFont;
    //被替换的字体
    private static Font curFontOld;
    //替换的字体
    private static Font curFontNew;

    //当前字体
    private static Font curFont1;
    private static Font curFont2;
    private static Font curFont3;
    private static Font curFont4;
    //是否改变字体类型
    private static bool isChangeStyle = false;

    //字体类型
    private static FontStyle curFontStyle;

    //是否增加字体大小
    private static bool isExpandSize = false;

    //字体大小增加的值
    private static int fontSizeDelta = 0;

    //window菜单下
    [MenuItem("Window/修改字体引用")]
    private static void ShowWindow()
    {
        ChangeFontWindow cw = GetWindow<ChangeFontWindow>(true, "修改字体引用");
        //(强迫症,看着舒服)
        cw.minSize = new Vector2(310, 400);
        cw.maxSize = new Vector2(310, 400);
    }

    private void OnGUI()
    {
        //向下空出5个像素
        GUILayout.Space(5);

        //创建是否改变当前字体开关
        isChangFont = EditorGUILayout.Toggle("是否改变当前字体引用位置", isChangFont);
        GUILayout.Space(5);

        //如果改变当前字体则创建字体文件选择框
        if (isChangFont)
        {
            curFont1 = (Font)EditorGUILayout.ObjectField("目标字体1", curFont1, typeof(Font), true);
            curFont2 = (Font)EditorGUILayout.ObjectField("目标字体2", curFont2, typeof(Font), true);
            curFont3 = (Font)EditorGUILayout.ObjectField("目标字体1", curFont3, typeof(Font), true);
            curFont4 = (Font)EditorGUILayout.ObjectField("目标字体2", curFont4, typeof(Font), true);
            GUILayout.Space(5);

            //创建是否改变字体类型开关
            isChangeStyle = EditorGUILayout.Toggle("是否改变字体类型", isChangeStyle);
            GUILayout.Space(5);

            //如果改变,则创建字体类型的枚举下拉框
            if (isChangeStyle)
            {
                curFontStyle = (FontStyle)EditorGUILayout.EnumPopup("字体类型", curFontStyle);
                GUILayout.Space(5);
            }

            //创建是否增加字体大小的开关
            isExpandSize = EditorGUILayout.Toggle("是否增加字体大小", isExpandSize);
            GUILayout.Space(5);

            //如果增加字体大小则创建增加字体大小值的滑条
            if (isExpandSize)
            {
                fontSizeDelta = (int)EditorGUILayout.Slider("增加字体大小的值", fontSizeDelta, -200, 200);
                GUILayout.Space(5);
            }

            //创建确认按钮
            if (GUILayout.Button("确认替换引用", GUILayout.Height(30), GUILayout.Width(300)))
            {
                Change();
            }
        }
        GUILayout.Space(5);
        isChangFontLibrary = EditorGUILayout.Toggle("是否改变字体引用字体库", isChangFontLibrary);

        if (isChangFontLibrary)
        {
            curFontOld = (Font)EditorGUILayout.ObjectField("被替换字体", curFontOld, typeof(Font), true);
            curFontNew = (Font)EditorGUILayout.ObjectField("替换字体", curFontNew, typeof(Font), true);
            GUILayout.Space(10);

            if (GUILayout.Button("确认替换字库", GUILayout.Height(30), GUILayout.Width(300)))
            {
                ChangeFontLibrary();
            }
        }
        GUILayout.Space(5);
        isSelectFont = EditorGUILayout.Toggle("是否查找字体被引用", isSelectFont);
        if (isSelectFont)
        {
            selectFont = (Font)EditorGUILayout.ObjectField("被查找字体", selectFont, typeof(Font), true);
            GUILayout.Space(10);
            if (GUILayout.Button("确认查找", GUILayout.Height(30), GUILayout.Width(300)))
            {
                SelectFont();
            }
        }
    }

    public static void SelectFont() 
    {
        if (Selection.objects == null || Selection.objects.Length == 0) { return; }

        //获取点中对象(包括子目录)所有UILabel组件
        Object[] labels = Selection.GetFiltered(typeof(UILabel), SelectionMode.Deep);
        foreach (Object item in labels)
        {
            UILabel label = (UILabel)item;

            if (isSelectFont)
            {
                if (label.bitmapFont == null && label.trueTypeFont != null)
                {
                    if (selectFont != null && label.trueTypeFont.name == selectFont.name)
                    {
                        Debug.Log("<----<");
                        Debug.Log(">>>>>>SelectfontName " + selectFont.name + "  objName " + label.name);
                        Debug.Log(">>>>>>fontPath " + AssetDatabase.GetAssetPath(label));
                        Debug.Log(">---->");
                    }
                }
            }

            //EditorUtility.SetDirty(item); //重要(有点像应用设置的意思)
        }
    }


    public static void ChangeFontLibrary() 
    {
        if (Selection.objects == null || Selection.objects.Length == 0) { return; }

        //获取点中对象(包括子目录)所有UILabel组件
        Object[] labels = Selection.GetFiltered(typeof(UILabel), SelectionMode.Deep);

        //赋值
        foreach (Object item in labels)
        {
            UILabel label = (UILabel)item;

            if (isChangFontLibrary)
            {
                if (label.bitmapFont == null && label.trueTypeFont != null)
                {
                    if (curFontOld != null && curFontNew != null && label.trueTypeFont.name == curFontOld.name)
                    {
                        label.trueTypeFont = curFontNew;
                    }
                    Debug.Log("<----<");
                    Debug.Log(">>>>>>fontPath " + AssetDatabase.GetAssetPath(label) + "  objName " + label.name);
                    Debug.Log(">>>>>>fontName " + curFontOld.name + "  new FontName " + curFontNew.name);
                    Debug.Log(">---->");
                }
            }

            EditorUtility.SetDirty(item); //重要(有点像应用设置的意思)
        }
    }

    public static void Change()
    {
        //如果鼠标没有选中物体则返回
        if (Selection.objects == null || Selection.objects.Length == 0) { return; }

        //获取点中对象(包括子目录)所有UILabel组件
        Object[] labels = Selection.GetFiltered(typeof(UILabel), SelectionMode.Deep);

        //赋值
        foreach (Object item in labels)
        {
            UILabel label = (UILabel)item;

            if (isChangFont) 
            {
                if (label.bitmapFont == null && label.trueTypeFont != null)
                {
                    if (curFont1 != null && label.trueTypeFont.name == curFont1.name)
                    {
                        label.trueTypeFont = curFont1;
                    }
                    else if (curFont2 != null && label.trueTypeFont.name == curFont2.name)
                    {
                        label.trueTypeFont = curFont2;
                    }
                    else if (curFont3 != null && label.trueTypeFont.name == curFont3.name)
                    {
                        label.trueTypeFont = curFont3;
                    }
                    else if (curFont4 != null && label.trueTypeFont.name == curFont4.name)
                    {
                        label.trueTypeFont = curFont4;
                    }
                    Debug.Log("<----<");
                    Debug.Log(">>>>>>fontPath " + AssetDatabase.GetAssetPath(label));
                    Debug.Log(">>>>>>fontName " + label.trueTypeFont.name);
                    Debug.Log(">---->");
                }
            }

            if (isChangeStyle) { label.fontStyle = curFontStyle; }

            if (isExpandSize) { label.fontSize += fontSizeDelta; }

            EditorUtility.SetDirty(item); //重要(有点像应用设置的意思)
        }
    }
}

标签: Unity3D-Editer

发表评论:

雨辰 joyimp|@2011-2017 京ICP备16030765号