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);
    }
}
相关推荐
HonestGoat3 小时前
Unity3d之碰撞体设置
unity
那个村的李富贵11 小时前
Unity自适应文本提示框:从原理到实战
unity·游戏引擎
HonestGoat11 小时前
Unity3d之鼠标光标
unity
WarPigs11 小时前
Unity人物翻越功能
unity·游戏引擎
游乐码12 小时前
Unity基础(四)向量相关
游戏·unity·游戏引擎
VT LI13 小时前
Cocos2d-x 引擎架构全面深度解析:从底层渲染到上层交互的系统性技术全景
游戏引擎·cocos·引擎架构
Kurisu57514 小时前
探灵直播2026最新官方正版免费下载 一键转存 永久更新 (看到速转存 资源随时走丢)
游戏·游戏引擎·游戏程序·动画·关卡设计
神码编程15 小时前
【Unity】MiniGame编辑器小游戏(十五)中国象棋局域网对战【Chinese Chess】(上)
unity·编辑器·游戏引擎·小游戏
伽蓝_游戏15 小时前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
郝学胜-神的一滴15 小时前
中级OpenGL教程 006:高光反射原理与 Shader 实现
c++·unity·godot·图形渲染·three.js·opengl·unreal