Unity 查找资源引用工具

Unity 查找资源引用工具

问题由来

  • 想查看组件被那些预设场景使用
  • 想查看图片被那些预设场景使用
  • 想查看资源被那些预设场景使用

解决方法

  • 遍历项目中所有资源判断是否引用

解决流程

  • 获取选中对象GUID
  • 使用Thread遍历项目中所有的资源
  • 判断资源是否包含GUID

使用方式

  • 选中脚本
  • 右键
  • ZQ -> 工具 -> 查找资源引用

源码

using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;

public static class FindreAssetFerencesTool
{
    static string[] assetGUIDs;
    static string[] assetPaths;
    static string[] allAssetPaths;
    static Thread thread;

    [MenuItem("ZQ/工具/查找资源引用", false)]
    [MenuItem("Assets/ZQ/工具/查找资源引用", false, 1)]
    static void FindreAssetFerencesMenu()
    {
        Debug.LogError("查找资源引用");

        if (Selection.assetGUIDs.Length == 0)
        {
            Debug.LogError("请先选择任意一个组件,再右键点击此菜单");
            return;
        }

        assetGUIDs = Selection.assetGUIDs;

        assetPaths = new string[assetGUIDs.Length];

        for (int i = 0; i < assetGUIDs.Length; i++)
        {
            assetPaths[i] = AssetDatabase.GUIDToAssetPath(assetGUIDs[0]);
        }

        allAssetPaths = AssetDatabase.GetAllAssetPaths();

        thread = new Thread(new ThreadStart(FindreAssetFerences));
        thread.Start();
    }


    static void FindreAssetFerences()
    {
        List<string> logInfo = new List<string>();
        string path;
        string log;
        for (int i = 0; i < allAssetPaths.Length; i++)
        {
            path = allAssetPaths[i];

            Debug.Log("正在查找文件:" + path);

            if (path.EndsWith(".prefab") || path.EndsWith(".unity"))
            {
                string content = File.ReadAllText(path);
                if (content == null)
                {
                    continue;
                }

                for (int j = 0; j < assetGUIDs.Length; j++)
                {
                    if (content.IndexOf(assetGUIDs[j]) > 0)
                    {
                        log = string.Format("{0} 引用了 {1}", path, assetPaths[j]);
                        logInfo.Add(log);
                    }
                }
            }
        }

        for (int i = 0; i < logInfo.Count; i++)
        {
            Debug.LogError(logInfo[i]);
        }

        Debug.LogError("选择对象引用数量:" + logInfo.Count);

        Debug.LogError("查找完成");
    }
}
posted @   邹强  阅读(2375)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示