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);
    }
}
相关推荐
爱搞虚幻的阿恺3 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
_Li.3 天前
Simulink - 6DOF (Euler Angles)
人工智能·算法·机器学习·游戏引擎·cocos2d
weixin_424294673 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames4 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy3258643644 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs4 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0124 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋4 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕4 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
星和月4 天前
Untiy使用说明
c#·游戏引擎