unity编辑器工具,输出使用的字体

csharp 复制代码
#if UNITY_EDITOR
using UnityEngine;
using UnityEngine.UI; // 引用UI命名空间以使用Text组件
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections.Generic;
using System.Linq;

public class CheckLegacyFontsTool : EditorWindow
{
    private Vector2 scrollPosition;
    private List<GameObject> legacyFontObjects = new List<GameObject>();

    // 添加菜单项
    [MenuItem("Tools/检查场景中的LegacyRuntime字体")]
    public static void ShowWindow()
    {
        GetWindow<CheckLegacyFontsTool>("字体检查工具");
    }

    void OnGUI()
    {
        GUILayout.Label("场景字体检查工具", EditorStyles.boldLabel);
        GUILayout.Space(10);

        if (GUILayout.Button("扫描当前场景"))
        {
            ScanCurrentScene();
        }

        GUILayout.Space(10);
        GUILayout.Label($"发现 {legacyFontObjects.Count} 个使用LegacyRuntime字体的对象:");

        // 滚动视图显示结果列表
        scrollPosition = GUILayout.BeginScrollView(scrollPosition);
        foreach (var obj in legacyFontObjects)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label(obj.name, GUILayout.Width(200));
            if (GUILayout.Button("选择", GUILayout.Width(60)))
            {
                Selection.activeGameObject = obj;
                EditorGUIUtility.PingObject(obj);
            }
            EditorGUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();
    }

    private void ScanCurrentScene()
    {
        legacyFontObjects.Clear();

        // 获取当前场景中所有的GameObject(更高效的查询可参考LINQ to GameObject思想[citation:4])
        Text[] allTexts = Resources.FindObjectsOfTypeAll<Text>();

        foreach (Text text in allTexts)
        {
            // 重要:确保只处理场景中的对象,排除预制体等资源
            if (!EditorUtility.IsPersistent(text.transform.root.gameObject) && text.gameObject.scene.IsValid())
            {
                // 检查字体名称是否为"LegacyRuntime"[citation:5]
                if (text.font != null && text.font.name.Contains("LegacyRuntime"))
                {
                    legacyFontObjects.Add(text.gameObject);
                    Debug.LogWarning($"发现LegacyRuntime字体: {GetGameObjectPath(text.gameObject)}", text.gameObject);
                }
            }
        }

        if (legacyFontObjects.Count == 0)
        {
            Debug.Log("扫描完成:当前场景中未发现使用LegacyRuntime字体的Text组件。");
        }
        else
        {
            Debug.Log($"扫描完成:共发现 {legacyFontObjects.Count} 个Text组件使用了LegacyRuntime字体。详细信息已输出至控制台。");
        }

        // 刷新编辑器窗口显示
        Repaint();
    }

    // 辅助方法:获取GameObject在场景中的完整路径
    private string GetGameObjectPath(GameObject obj)
    {
        if (obj == null) return "";
        string path = obj.name;
        while (obj.transform.parent != null)
        {
            obj = obj.transform.parent.gameObject;
            path = obj.name + "/" + path;
        }
        return path;
    }
}
#endif
相关推荐
袁代码1 小时前
【项目分享】把Claude、编辑器和浏览器装进终端
编辑器
wild-civil1 小时前
解决Keil 生成的文件在 VSCode 乱码问题(自动识别,不用手动改编码)
ide·vscode·stm32·编辑器
游乐码16 小时前
UnityGUI(五)GUI控件综合使用
开发语言·unity·c#
LF男男16 小时前
TshitBullect.cs
unity
霜落花轻扬20 小时前
VSCode全局搜索内容不全
ide·vscode·编辑器
CSDN官方博客1 天前
【重要公告】Markdown编辑器改版上线,体验全面升级!
编辑器
游乐码1 天前
Unity(十六)切换场景及鼠标相关
unity·游戏引擎
MuYiLuck1 天前
02-VSCode插件与Trae原生AI编辑器实战教程
vscode·编辑器·trae·trae solo
FakeEnd1 天前
Unity开发笔记6
笔记·unity·游戏引擎