Unity编辑器扩展之Text组件中字体替换工具

想要批量化替换项目预制体资源中Text组件引用的Font字体文件,可以采用以下步骤。

1、在项目的Editor文件中,新建一个名为FontToolEditor的C#脚本文件,然后把以下代码复制粘贴到新建的FontToolEditor的C#脚本文件中。

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Globalization;

public class FontToolEditor : EditorWindow
{
    private const string MENU_MAIN_WINDOW = "Tools/Font替换";

    private List<Font> fontList = new List<Font>() { null };
    public Font toChange;
    static Font toChangeFont;
    public string path = "Assets/";
    static string path1;
    public int num = 0;
    [MenuItem(MENU_MAIN_WINDOW)]
    public static void Open()
    {
        EditorWindow.GetWindow(typeof(FontToolEditor), true);
    }

    void OnGUI()
    {
        GUILayout.Space(5);
        GUILayout.Label("选择老字体", GUILayout.Width(100f));
        num = EditorGUILayout.IntField("字体个数", num);
        AddMonsScriptList();

        GUILayout.Space(20);

        toChange = (Font)EditorGUILayout.ObjectField("选择更换新字体", toChange, typeof(Font), true, GUILayout.MinWidth(100));
        toChangeFont = toChange;

        GUILayout.Space(20);

        GUILayout.Label("路径", GUILayout.Width(50f));

        GUILayout.BeginHorizontal();
        {
            if (GUILayout.Button("浏览"))
            {
                path = EditorUtility.OpenFolderPanel("窗口标题", Application.streamingAssetsPath, "");
                int index = path.IndexOf("Assets");
                path = path.Substring(index, path.Length - index) + "/";
            }
        }

        GUILayout.EndHorizontal();

        path1 = path;
        GUILayout.TextField(path);
        if (GUILayout.Button("确认更换"))
        {
            Init();
        }
    }
    private void AddMonsScriptList()
    {
        int scriptListCount = fontList.Count - 1;
        for (int i = 0; i < num; i++)
        {
            if (scriptListCount < i)
            {
                //给脚本添加一个空值,为了界面好看,也为了下面缓存脚本
                fontList.Add(null);
            }
            fontList[i] = (Font)EditorGUILayout.ObjectField(fontList[i], typeof(Font), true);
        }
    }

    private void Init()
    {
        List<GameObject> prefabs = new List<GameObject>();
        var resourcesPath = path1;
        var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);

        for (int i = 0; i < absolutePaths.Length; i++)
        {
            EditorUtility.DisplayProgressBar("字体转换中...", "字体转换中...", (float)i / absolutePaths.Length);
            string path = absolutePaths[i].Replace("\\", "/");
            GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;

            //替换font
            Text[] labels = prefab.GetComponentsInChildren<Text>(true);
            for (int q = 0; q < labels.Length; q++)
            {
                if (labels[q].font)
                {
                    for (int w = 0; w < fontList.Count; w++)
                    {
                        if (fontList[w].name == labels[q].font.name)
                        {
                            int fontSize = labels[q].fontSize;
                            var fonStyle = labels[q].fontStyle;
                            labels[q].font = toChangeFont;
                            labels[q].fontSize = fontSize;
                            labels[q].fontStyle = fonStyle;
                            EditorUtility.SetDirty(labels[q]);
                        }
                    }

                }
            }
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }
}

2、保存FontToolEditor脚本文件中代码,回到Unity中,点击Unity主界面左上角中Tools然后再点击Font替换选项。

3、选择好老字体,也就是需要被替换掉的字体,再然后需要选择好需要更换的新字体,再然后选择替换路径(也就是这个选择路径下的预支体才会进行字体替换),最后点击确认更换就可以。

相关推荐
无限大.11 分钟前
前端知识速记:节流与防抖
前端
十八朵郁金香13 分钟前
【VUE案例练习】前端vue2+element-ui,后端nodo+express实现‘‘文件上传/删除‘‘功能
前端·javascript·vue.js
学问小小谢17 分钟前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
LCG元42 分钟前
Vue.js组件开发-实现全屏图片文字缩放切换特效
前端·javascript·vue.js
还是鼠鼠2 小时前
图书管理系统 Axios 源码__新增图书
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
maply4 小时前
VSCode 中的 Git Graph扩展使用详解
ide·git·vscode·编辑器·扩展
还是鼠鼠5 小时前
图书管理系统 Axios 源码 __删除图书功能
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
花花鱼5 小时前
vscode script 中间的function import等关键字 先高亮,然后又灰了,并且按ctrl+/ 注释以html的形式,导致报错处理
ide·vscode·编辑器
轻口味5 小时前
Vue.js `Suspense` 和异步组件加载
前端·javascript·vue.js
m0_zj6 小时前
8.[前端开发-CSS]Day08-图形-字体-字体图标-元素定位
前端·css