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
相关推荐
广东帝工21 小时前
桥梁智能防撞主动预警系统——架构、体系、预警机制
编辑器
A小调的码农21 小时前
OPENOCD+MSYS+VSCODE:从“灯不亮“到“终于亮了“
ide·vscode·stm32·单片机·编辑器
AI的探索之旅21 小时前
让 Claude Code 直接操刀画原理图和 PCB:VSCode 插件接外部 API 全流程
linux·ide·vscode·嵌入式硬件·编辑器
广东帝工1 天前
桥梁防撞(防船撞)智能预警系统
编辑器
玖玥拾2 天前
C# 语言进阶(十四)Unity UI界面开发 + 网络消息联动
服务器·开发语言·网络·ui·unity·c#·游戏引擎
野区捕龙为宠2 天前
Unity 持久化数据
unity·游戏引擎·lucene
郝学胜-神的一滴2 天前
[简化版 GAMES 101] 计算机图形学 17:纹理技术从基础原理到多场景实战应用
c++·unity·游戏引擎·图形渲染·three.js·opengl·unreal
_ZHOURUI_H_3 天前
MyFramework: 同样是 Unity 游戏开发框架,和 HTFramework 的设计取向有什么不同?(下)
unity·游戏引擎·游戏开发·游戏ui·手游开发
HhzZzzzz_3 天前
萨科微Slkor2026年7月10日每日芯闻。
人工智能·智能手机·编辑器
小林ixn3 天前
Node.js 文件系统与路径处理:从 API 到工程化实战
node.js·编辑器·vim