unity ugui一键替换ui字体

一、内容

webgl端的unity自带字体在网页上不支持中文,需要把官方字体替换成准备好的字体.在编辑器下调用,避免一个个去手动修改text的字体


二、使用步骤


1.使用方式

把下面脚本你放到Editor文件夹下,在编辑器上方的"Tools/换字体",调用


2.代码

using UnityEditor;

using UnityEngine;
using UnityEngine.UI;

public class ChangeFontWindow : EditorWindow
{
    [MenuItem("Tools/换字体")]
    public static void Open()
    {
        EditorWindow.GetWindow(typeof(ChangeFontWindow) , true);
    }

    public Font toChange;
    static Font toChangeFont;

    void OnGUI()
    {
        toChange = (Font)EditorGUILayout.ObjectField("选择需要更换的字体" , toChange , typeof(Font) , true , GUILayout.MinWidth(100));
        toChangeFont = toChange;
        if (GUILayout.Button("确认更换"))
        {
            Change();
        }
    }

    public static void Change()
    {
        Object[] Texts = Selection.GetFiltered(typeof(Text) , SelectionMode.Deep);
        foreach (Object text in Texts)
        {
            if (text)
            {
                //如果是UGUI讲UILabel换成Text就可以  
                Text TempText = (Text)text;
                Undo.RecordObject(TempText , TempText.gameObject.name);
                TempText.font = toChangeFont;
                TempText.fontStyle = FontStyle.Bold;
                Debug.Log(text.name + ":" + TempText.text);
                EditorUtility.SetDirty(TempText);
            }
        }
    }
}

替换assets文件夹下prefab的text字体

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


}


总结

没了,代码都是常见的代码.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值