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
相关推荐
WarPigs1 小时前
Unity未激活的物体无法响应事件?
java·unity·游戏引擎
LabVIEW开发1 小时前
LabVIEW图标编辑器文字模糊现象
ui·编辑器·labview·labview知识·labview功能·labview程序
玖玥拾10 小时前
Unity3D RPG 入门项目(十一)主角完整战斗、怪物自动刷新、四大类型技能系统
游戏·3d·unity·游戏引擎
Behaviour12 小时前
Unity AI 生态横评对比:官方 AI Beta / Unity CLI / 团结 Codely / 社区 MCP 四大阵营选型
人工智能·unity·ai·游戏引擎·aigc·ai编程
on_pluto_19 小时前
【debug】vscode 和 codex 不兼容问题
ide·人工智能·vscode·编辑器
Madokaly1 天前
基类 DG.Tweening.Tween的源码解读
unity
Web - Anonymous1 天前
Vue3 + Element Plus 集成 JSON Editor 实现实时校验 JSON 编辑器(高亮/暗亮主题/分栏回显/组件复用) - 附完整示例
vue.js·编辑器·json
Behaviour1 天前
Unity游戏之Jenkins的安装部署应用
游戏·unity·jenkins
大Mod_abfun1 天前
Unity案例10-水晶矿石碰撞碎裂案例
unity·游戏引擎