unity修改默认字体

unity 脚本 同时被 2 个专栏收录
70 篇文章 0 订阅
3 篇文章 0 订阅

 注意问题,切换场景时需要重新配置一下*如若出现报错,再次配置一下即可

using UnityEngine;
using UnityEditor;
using System;
using UnityEngine.UI;
using UnityEngine.Windows;

public class SetDefaultFont : EditorWindow
{
    private static Font m_font;
    private static EditorWindow window;

    public static Font Font
    {
        get
        {
            return m_font;
        }
    }

    [MenuItem("CustomTool/设置默认字体")]
    public static void OpenWindow()
    {
        window = GetWindow(typeof(SetDefaultFont));
        window.minSize = new Vector2(500, 300);
        m_font = ToolCacheManager.GetFont();
    }

    private void OnGUI()
    {
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("选择默认字体");
        EditorGUILayout.Space();
        m_font = (Font)EditorGUILayout.ObjectField(m_font, typeof(Font), true);
        EditorGUILayout.Space();
        if (GUILayout.Button("确定"))
        {
            ToolCacheManager.SaveFont(m_font);
            window.Close();
        }
    }
}
public class ToolCacheManager
{
    private static readonly string cachePath =
        Application.dataPath.Substring(0, Application.dataPath.Length - 6) + "Library/BlueToolkitCache/";

    private static void Init()
    {
        if (!Directory.Exists(cachePath))
        {
            Directory.CreateDirectory(cachePath);
        }
    }

    public static void SaveFont(Font font)
    {
        FontData data = ScriptableObject.CreateInstance<FontData>();
        data.defaultFont = font;
        AssetDatabase.CreateAsset(data, "Assets/front/FontData.asset");
    }

    public static Font GetFont()
    {
        FontData data = AssetDatabase.LoadAssetAtPath<FontData>("Assets/front/FontData.asset");
        return data.defaultFont;
    }
}
[System.Serializable]
public class FontData : ScriptableObject
{
    [SerializeField]
    public Font defaultFont;
}

public class UnityUIEvent
{
    [InitializeOnLoadMethod]
    private static void Init()
    {
        Action OnEvent = delegate
        {
            ChangeDefaultFont();
        };

        EditorApplication.hierarchyChanged += delegate ()
        {
            OnEvent();
        };
    }

    private static void ChangeDefaultFont()
    {
        if (Selection.activeGameObject != null)
        {
            Text text = Selection.activeGameObject.GetComponent<Text>();
           
            if (text != null)
            {
                text.font = ToolCacheManager.GetFont();
            }
            //********** 
            Text[] text_arr = Selection.activeGameObject.transform.GetComponentsInChildren<Text>();
            if (text_arr != null)
            {
                for (int i = 0; i < text_arr.Length; i++)
                {
                    text_arr[i].font= ToolCacheManager.GetFont();
                }
            }
        }

    }
}


 

  • 2
    点赞
  • 1
    评论
  • 2
    收藏
  • 扫一扫,分享海报

评论1
请先登录 后发表评论~
©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值