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
相关推荐
是嘟嘟啊2 小时前
【Unity DOTS】EntityCommandBuffer 与 JobHandle
unity·ecs·dots·unity dots·job·burst·burstjob
xcLeigh3 小时前
Unity基础:Camera相机组件详解——透视投影与正交投影的区别
数码相机·unity·游戏引擎
玖玥拾5 小时前
Unity3D RPG 入门项目(二)移动 Bug 修复、环绕相机、NPC 任务系统、UI 穿透处理
unity·游戏引擎
Yasin Chen8 小时前
Unity UGUI RectMask2D 原理
unity·游戏引擎
hashiqimiya8 小时前
unity---unity编译手机apk人物角色移动
unity·游戏引擎
LONGZETECH9 小时前
传统实训 vs 仿真实训:从技术架构拆解新能源汽车教学的范式之变
c语言·3d·unity·架构·汽车
啦啦啦!10 小时前
虚拟机如何接入Codex并配置中转站(vscode插件版)
linux·vscode·ubuntu·编辑器
℡枫叶℡1 天前
Unity - 图集大小与内存占用
unity·图集内存
_ZHOURUI_H_1 天前
Unity MyFramework 用法说明(二十五):使用 AtlasManager 统一管理图集与 Sprite 引用
ui·unity·游戏引擎·unity3d·游戏开发
新手unity自用笔记1 天前
Unity网络基础_2
unity·游戏引擎