美文网首页
Unity 一键更换字体

Unity 一键更换字体

作者: 菜鸟的笔记 | 来源:发表于2020-08-28 15:44 被阅读0次
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;
using System.IO;

public class ChangeFontWindow : EditorWindow
{
    [MenuItem("ProTool/Multilingual/换字体")]
    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();
        }
    }
    private static string PREFABPATH ; //所有预设存放位置路径
    public static void Change()
    {
        PREFABPATH = Application.dataPath + "\\Project3D\\BundleData\\UI\\Prefab";
        DirectoryInfo directoryInfo = Directory.CreateDirectory(PREFABPATH);
        FileInfo[] fileInfos = directoryInfo.GetFiles("*.prefab", SearchOption.AllDirectories);
        for (int i = 0; i < fileInfos.Length; i++)
        {
            string assetPath = fileInfos[i].FullName.Replace("\\", "/");
            assetPath = assetPath.Replace(Application.dataPath, "Assets");
            UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetsAtPath(assetPath);
            foreach (Object text in objs)
            {
                if (text.GetType() == typeof(Text))
                {
                    Text TempText = (Text)text;
                    Undo.RecordObject(TempText, TempText.gameObject.name);
                    TempText.font = toChangeFont;
                    EditorUtility.SetDirty(TempText);
                }
            }
        }

    }
}

相关文章

网友评论

      本文标题:Unity 一键更换字体

      本文链接:https://www.haomeiwen.com/subject/bxplsktx.html