常用功能------全局更改text字体

unity中全局更改text字体

1、废话不多说,直接上代码,忘了学习了某位博主的代码,就不加引用了。

/*****************************************************************
 *Author: ZhaoZhen
 *UnityVersion: 2020.3.1f1
 *Function: 
 *******************************************************************/
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.UI;
using System.Collections.Generic;

namespace zhaozhen
{
    /// <summary>
    /// 更换场景中的字体
    /// </summary>
    public class ChangeFontWindow : EditorWindow
    {
        [MenuItem("Tools/更换字体设置")]
        public static void Open()
        {
            EditorWindow.GetWindow(typeof(ChangeFontWindow));
        }

        Font toChange;
        static Font toChangeFont;
        FontStyle toFontStyle;
        static FontStyle toChangeFontStyle;

        void OnGUI()
        {
            toChange = (Font)EditorGUILayout.ObjectField(toChange, typeof(Font), true, GUILayout.MinWidth(100f));
            toChangeFont = toChange;
            toFontStyle = (FontStyle)EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f));
            toChangeFontStyle = toFontStyle;
            if (GUILayout.Button("更换Hierarchy面板下所有text字体"))
            {
                ChangeFont_Scene();
            }
            if (GUILayout.Button("更换预设物中所有text字体"))
            {
                ChangeFont_Prefab();
            }
        }

        /// <summary>
        /// 更换场景中Hierarchy面板下所有text字体方法
        /// </summary>
        public static void ChangeFont_Scene()
        {
            //寻找Hierarchy面板下所有的Text
            var tArray = Resources.FindObjectsOfTypeAll(typeof(Text));
            for (int i = 0; i < tArray.Length; i++)
            {
                Text t = tArray[i] as Text;
                //这个很重要,如果没有这个代码,unity是不会察觉到编辑器有改动的,自然设置完后直接切换场景改变是不被保存的 ,
                //如果不加这个代码  在做完更改后 自己随便手动修改下场景里物体的状态 在保存就好了 
                Undo.RecordObject(t, t.gameObject.name);
                t.font = toChangeFont;
                t.fontStyle = toChangeFontStyle;
                //相当于让他刷新下 不然unity显示界面还不知道自己的东西被换掉了  还会呆呆的显示之前的东西
                EditorUtility.SetDirty(t);
            }
            Debug.Log("Succed,Hierarchy面板下text字体更换成功!");
        }

        /// <summary>
        /// 更换预制件中text字体
        /// </summary>
        public static void ChangeFont_Prefab()
        {
            List<Text[]> textList = new List<Text[]>();//表示获取到的预制件中的text组件列表
                                                       //获取Asset文件夹下所有Prefab的GUID
            string[] ids = AssetDatabase.FindAssets("t:Prefab");
            string tmpPath;//表示获取到的预制件的位置路径
            GameObject tmpObj;//表示获取到的预制件物体
            Text[] tmpArr;
            for (int i = 0; i < ids.Length; i++)
            {
                tmpObj = null;
                tmpArr = null;
                //根据GUID获取路径
                tmpPath = AssetDatabase.GUIDToAssetPath(ids[i]);
                if (!string.IsNullOrEmpty(tmpPath))//路径不为空
                {
                    //根据路径获取Prefab(GameObject)
                    tmpObj = AssetDatabase.LoadAssetAtPath(tmpPath, typeof(GameObject)) as GameObject;
                    //判断是否有text组件
                    if (tmpObj.GetComponentsInChildren<Text>().Length != 0)
                    {
                        //读入预制体
                        var path = tmpPath;
                        var instance = PrefabUtility.LoadPrefabContents(path);
                        if (instance != null)
                        {
                            //获取Prefab及其子物体孙物体.......的所有Text组件
                            tmpArr = instance.GetComponentsInChildren<Text>();
                            for (int j = 0; j < tmpArr.Length; j++)
                            {
                                tmpArr[j].font = toChangeFont;
                                tmpArr[j].fontStyle = toChangeFontStyle;
                            }
                        }
                        // 保存预制体
                        PrefabUtility.SaveAsPrefabAsset(instance, path);
                        PrefabUtility.UnloadPrefabContents(instance);
                    }
                }
            }
            Debug.Log("Succed,预制件字体更换成功!");
        }
    }
}

2、结果
在标题栏对应找Tools/更换字体设置,点击更改字体,成功执行。
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值