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);
    }
}
相关推荐
小李也疯狂4 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的4 小时前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y4 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤4 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里4 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ5 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
YigAin6 小时前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al7 小时前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
天人合一peng8 小时前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala12 小时前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎