Unity实现点击Console消息自动选中预制体的方法

直接上代码:

官方文档路径:重要的类 - Debug - Unity 手册

参考代码(实际上很简单,但不知为什么网上搜出来的教程要么实现复杂,要么就是答非所问):

cs 复制代码
Debug.LogWarning($"存在引用----文件 <a href=\"{f}\" line=\"1\">{f}</a>",  这里填入需要定位的GameObject对象);

// 实例参考:
private UnityEngine.Object obj;
Debug.LogWarning($"存在引用----文件 <a href=\"{f}\" line=\"1\">{f}</a>", obj);
// 字符串功能说明: <a href=\"{f}\" line=\"1\">{f}</a>
// 该字符串中的f为需要定位的obj对象的相对路径,例如 "Assets/Res/Prefabs/UI/Test.prefab" ,使用该字符串在点击Console消息后,在Console下方的详细信息中点击资源路径时,可直接打开对应的预制体

最后附带上资源文件引用查询代码参考:

cs 复制代码
using System.Collections;
using System.IO;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 查找资源引用工具
/// </summary>
public class FindAssetReferenceTool
{
    private const string FindTip = "未查询到引用该文件的资源";
    private const string AssetsPath = "Assets/";
    private static bool findResult;
    private static Object obj;
    private static float currentProcess;
    [MenuItem("Assets/查找引用该资源的所有资源")]
    private static void FindAssetReference()
    {
        findResult = true;
        obj = Selection.activeObject;
        string p = AssetDatabase.GetAssetPath(obj);
        string guid = AssetDatabase.AssetPathToGUID(p);
        Debug.Log($"当前文件所在路径:{p}");
        Debug.Log($"当前文件GUID:{guid}");
        EditorCoroutineUtility.StartCoroutineOwnerless(TraverseAllFile(guid, AssetsPath));
    }

    private static bool CheckReferenceInfo(string guid, string filePath)
    {
        return File.ReadAllText(filePath).Contains(guid);
    }

    private static IEnumerator TraverseAllFile(string guid, string path)
    {
        yield return null;
        currentProcess = 0;
        while (true)
        {
            currentProcess++;
            string[] files = Directory.GetFiles(path);
            string[] directorys = Directory.GetDirectories(path);
            bool Cancel = EditorUtility.DisplayCancelableProgressBar($"正常查询对 {obj.name} 资产的所有引用", path, currentProcess / directorys.Length);
            foreach (string dir in directorys)
            {
                yield return TraverseAllFile(guid, dir);
                if(Cancel) break;
            }

            if (Cancel) break;
            foreach (string f in files)
            {
                if (f.EndsWith(".meta")) continue;
                if (CheckReferenceInfo(guid, f))
                {
                    Debug.LogWarning($"存在引用----文件 <a href=\"{f}\" line=\"1\">{f}</a>", (obj as GameObject).transform);
                    findResult = false;
                }
                if (Cancel) break;
            }
            break;
        }
        EditorUtility.ClearProgressBar();
        if (findResult && AssetsPath.CompareTo(path) == 0)
            Debug.LogWarning(FindTip);
    }
}
相关推荐
JIes__10 小时前
Unity(二)——Resources资源动态加载
unity·游戏引擎
地狱为王11 小时前
Unity使用NovaSR将沉闷的16kHz音频升频成清晰的48kHz音频
unity·游戏引擎·音视频·novasr
dzj202113 小时前
Unity中使用LLMUnity遇到的问题(二)——LLMUnity脚本学习和探索
unity·llmunity
警醒与鞭策17 小时前
Cursor Agent Skill 原理及LLM , Agent, MCP ,Skill区别
android·unity·ai·cursor
孟无岐1 天前
【Laya】Socket 使用指南
websocket·typescript·游戏引擎·游戏程序·laya
tealcwu1 天前
【Unity资源】Unity MCP 介绍
unity·游戏引擎
晚霞的不甘2 天前
Flutter for OpenHarmony 引力弹球游戏开发全解析:从零构建一个交互式物理小游戏
前端·flutter·云原生·前端框架·游戏引擎·harmonyos·骨骼绑定
Thomas_YXQ2 天前
Unity3D中提升AssetBundle加载速度的详细指南
java·spring boot·spring·unity·性能优化·游戏引擎·游戏开发
Miss_SQ2 天前
Unity接入AI—Deepseek,数据流式传输
unity·ai