Unity 替換所有字體工具(包括Prefab)


1.方便快捷替換所有text字體  

#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
/// <summary>
/// 需要指定字體的路徑 名字   路徑放到Resources文件夾中
/// </summary>
public class MyTool : EditorWindow
{
    //替換場景內的所有字體
    [MenuItem("FontTools/替換場景中所有text字體")]
    public static void ChangeFont_Scene()
    {
        //加載目標字體  "目標字體的名字"      
        Font targetFont = Resources.Load<Font>("簡啟體");
        //獲取場景所有激活物體
        //GameObject[] objs = FindObjectsOfType(typeof(GameObject)) as GameObject[];
        //獲取場景所有物體
        GameObject[] allObj = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
        Text tmpText;
        int textCount = 0;
        for (int i = 0; i < allObj.Length; i++)
        {
            //帶有Text組件的GameObject,替換字體
            tmpText = allObj[i].GetComponent<Text>();
            if (tmpText != null)
            {
                textCount++;
                tmpText.font = targetFont;
                //在此擴展,可以給添加外邊框,也可以根據需求進行其他操作
                //allObj[i].AddComponent<Outline>();
            }
        }
        Debug.Log("<color=yellow> 當前場景共有:物體 </color>" + allObj.Length + "<color=yellow> 個,Text組件 </color>" + textCount + "<color=green> 個 </color>");
    }
    //替換資源文件夾中全部Prefab的字體
    [MenuItem("FontTools/替換預設物中所有text字體")]
    public static void ChangeFont_Prefab()
    {
        Font targetFont = Resources.Load<Font>("簡啟體");
        List<Text[]> textList = new List<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;
                if (tmpObj != null)
                {
                    //獲取Prefab及其子物體孫物體.......的所有Text組件
                    tmpArr = tmpObj.GetComponentsInChildren<Text>();
                    if (tmpArr != null && tmpArr.Length > 0)
                        textList.Add(tmpArr);
                }
            }
        }
        //替換所有Text組件的字體
        int textCount = 0;
        for (int i = 0; i < textList.Count; i++)
        {
            for (int j = 0; j < textList[i].Length; j++)
            {
                textCount++;
                textList[i][j].font = targetFont;
            }
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.Log("<color=yellow> 當前ProJect共有:Prefab </color>" + ids.Length + "<color=yellow> 個,帶有Text組件Prefab </color>" + textList.Count + "<color=green> 個,Text組件 </color>" + textCount + "<color=green> 個 </color>");
    }
}
#endif 

 

因為不想一個一個替換所以記錄一下,方便使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM