Unity封装Debug.Log导致代码定位失准的解决办法

news2024/9/25 15:21:53

在这里插入图片描述
笔者通过翻资料,实现了这样的一个编辑器,虽然无法彻底消除指定的日志信息
但是可以实现”双击日志不跳转到这里的任意一个文件“

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

namespace AirEditor
{

    public class LogFilterInfo : ScriptableSingleton<LogFilterInfo>
    {
        public List<string> CustomInfos = new List<string>();
        public List<string> BuiltInInfos = new List<string>();
        public bool ShowBuiltIn = false;
    }

    public static class HideLog
    {
        static bool ContainFile(string str)
        {
            bool exist = false;
            foreach (var s in LogFilterInfo.instance.BuiltInInfos)
            {
                if (exist) break;
                exist |= str.Contains(s);
            }
            foreach (var s in LogFilterInfo.instance.CustomInfos)
            {
                if (exist) break;
                exist |= str.Contains(s);
            }
            return exist;
        }

        [UnityEditor.Callbacks.OnOpenAsset(0)]
        static bool OnOpenAsset(int instanceID, int line)
        {
            //Debug.Log("OnOpenAsset");
            string stackTrace = GetStackTrace();
            if (!string.IsNullOrEmpty(stackTrace))
            {
                System.Text.RegularExpressions.Match matches = System.Text.RegularExpressions.Regex.Match(stackTrace, @"\(at (.+)\)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                string pathLine = "";
                while (matches.Success)
                {
                    pathLine = matches.Groups[1].Value;

                    if (!ContainFile(pathLine))
                    {
                        int splitIndex = pathLine.LastIndexOf(":");
                        string path = pathLine.Substring(0, splitIndex);
                        line = System.Convert.ToInt32(pathLine.Substring(splitIndex + 1));
                        string fullPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("Assets"));
                        fullPath = fullPath + path;
                        UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(fullPath.Replace('/', '\\'), line);
                        break;
                    }
                    matches = matches.NextMatch();
                }
                return true;
            }
            return false;
        }

        static string GetStackTrace()
        {
            var ConsoleWindowType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow");
            var fieldInfo = ConsoleWindowType.GetField("ms_ConsoleWindow", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
            var consoleInstance = fieldInfo.GetValue(null);
            if (consoleInstance != null)
            {
                if ((object)UnityEditor.EditorWindow.focusedWindow == consoleInstance)
                {
                    var ListViewStateType = typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.ListViewState");
                    fieldInfo = ConsoleWindowType.GetField("m_ListView", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    var listView = fieldInfo.GetValue(consoleInstance);
                    fieldInfo = ListViewStateType.GetField("row", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    int row = (int)fieldInfo.GetValue(listView);
                    fieldInfo = ConsoleWindowType.GetField("m_ActiveText", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    string activeText = fieldInfo.GetValue(consoleInstance).ToString();
                    return activeText;
                }
            }
            return null;
        }

    }
    public class LoggerFilter : EditorWindow
    {

        [MenuItem("LoggerFilter", menuItem = "AirFramework/LoggerFilter")]
        static void ShowWindow()
        {
            var window = GetWindow<LoggerFilter>();
            window.Show();
        }

        void DrawTextPathArea(string path, bool expand = true)
        {
            string path0 = Path.Combine(EditorHelper.ProjectPath, path);
            bool exist = File.Exists(path0);
            if (!exist)
            {
                Color temp = GUI.color;
                GUI.color = Color.red;

                GUILayout.TextArea(path, GUILayout.ExpandWidth(expand));
                GUI.color = temp;
            }
            else
            {
                GUILayout.TextArea(path, GUILayout.ExpandWidth(expand));
            }
        }
        void DragFileToPath(ref string str)
        {
            // 路径拖拽
            if (Event.current.type == EventType.DragUpdated)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                Event.current.Use();
            }
            else if (Event.current.type == EventType.DragPerform)
            {
                DragAndDrop.AcceptDrag();
                foreach (string draggedPath in DragAndDrop.paths)
                {
                    str = draggedPath;
                    break;
                }
                Event.current.Use();
            }

        }
        void DrawList(ref List<string> list, ref Vector2 pos)
        {
            pos = EditorGUILayout.BeginScrollView(pos);
            foreach (var item in list)
            {
                GUILayout.BeginHorizontal();
                DrawTextPathArea(item);
                if (GUILayout.Button("Open", GUILayout.ExpandWidth(false)))
                {
                    Application.OpenURL(EditorHelper.ProjectPath + "/" + item);
                }
                if (GUILayout.Button("Remove", GUILayout.ExpandWidth(false)))
                {
                    removeQueue.Enqueue(item);
                }
                GUILayout.EndHorizontal();
            }
            EditorGUILayout.EndScrollView();
            while (removeQueue.Count > 0) list.Remove(removeQueue.Dequeue());
        }
        void AddTo(ref List<string> list, string str)
        {
            if (str == null || str == string.Empty) return;
            list.RemoveAll((x) => x == str);
            list.Add(str);
        }

        Vector2 POS, POS2;
        string add = null;
        Queue<string> removeQueue = new Queue<string>();
        int selected = 0;
        readonly string[] bar = new string[2] { "Custom", "BuiltIn" };



        private void OnGUI()
        {
            selected = GUILayout.Toolbar(selected, bar);
            //处理路径拖拽
            DragFileToPath(ref add);
            GUILayout.BeginHorizontal();
            //路径输入
            add = EditorGUILayout.TextField("Ignore FileName", add, GUILayout.ExpandWidth(true));

        
            switch (selected)
            {
                case 0:
                    {
                        if (GUILayout.Button("Add",GUILayout.ExpandWidth(false)))
                            AddTo(ref LogFilterInfo.instance.BuiltInInfos, add);
                        GUILayout.EndHorizontal();

                        DrawList(ref LogFilterInfo.instance.BuiltInInfos, ref POS);
                        break;
                    }
                case 1:
                    {
                        if (GUILayout.Button("Add", GUILayout.ExpandWidth(false)))
                            AddTo(ref LogFilterInfo.instance.CustomInfos, add);

                        GUILayout.EndHorizontal();
                        DrawList(ref LogFilterInfo.instance.CustomInfos, ref POS2);
                        break;
                    }
            }
          
       


        }


    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/907674.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

浅谈java自定义中类两个对象的比较

目录 实现比较两个对象是否相同 1.前置代码 1.学生类 2.示例 3.输出 4.原因 2.那么我们要怎么做呢? 1.对Student类中重新实现quals方法(即对equals方法重写) 2.完整代码如下: 3.具体操作 4.演示 1.示例 2.输出 3.原因 实现比较两个对象的大小 第一种: 用…

智慧小区建设方案【47页PPT】

导读&#xff1a;原文《智慧小区建设方案【47页PPT】》&#xff08;获取来源见文尾&#xff09;&#xff0c;本文精选其中精华及架构部分&#xff0c;逻辑清晰、内容完整&#xff0c;为快速形成售前方案提供参考。 部分页面&#xff1a; 喜欢文章&#xff0c;您可以关注评论转…

实现图片的动态连续展示 JAVA

目录 1、前言&#xff1a;2、图片的展示以及自动关闭&#xff1a;3、动画的连续展示&#xff1a; 1、前言&#xff1a; 要实现动画的流畅展示需要在能展示图片的基础上对图片进行关闭&#xff0c;再切换下一张图片&#xff0c;这要关闭窗口&#xff0c;与延时函数以及while函数…

从SVG到Canvas:选择最适合你的Web图形技术

SVG 和 Canvas 都是可以在 Web 浏览器中绘制图形的技术。 众所周知&#xff0c; icon 通常使用 svg&#xff08;如 iconfont&#xff09;&#xff0c;而交互式游戏采用 Canvas。二者具体的区别是什么&#xff1f;该如何选择&#xff1f; 声明式还是命令式&#xff1f;绘制的图形…

C语言:库函数atoi及其模拟实现

i&#xff1a; atof是C语言标准库中的一个函数&#xff0c;用于将字符串转换为对应的浮点数/整形数。 函数接受一个参数str&#xff0c;该参数是一个指向以null结尾的字符串的指针。atof函数会尝试将这个字符串转换为一个浮点数&#xff0c;并返回转换后的结果。 要注意的是&a…

【3D激光SLAM】LOAM源代码解析--laserOdometry.cpp

系列文章目录 【3D激光SLAM】LOAM源代码解析–scanRegistration.cpp 【3D激光SLAM】LOAM源代码解析–laserOdometry.cpp 写在前面 本系列文章将对LOAM源代码进行讲解&#xff0c;在讲解过程中&#xff0c;涉及到论文中提到的部分&#xff0c;会结合论文以及我自己的理解进行解…

Numpy入门(4)— 保存和导入文件

NumPy保存和导入文件 4.1 文件读写 NumPy可以方便的进行文件读写&#xff0c;如下面这种格式的文本文件&#xff1a; # 使用np.fromfile从文本文件a.data读入数据 # 这里要设置参数sep &#xff0c;表示使用空白字符来分隔数据 # 空格或者回车都属于空白字符&#xff0c;读…

leetcode 542. 01 Matrix(01矩阵)

矩阵中只有0&#xff0c;1值&#xff0c;返回每个cell到最近的0的距离。 思路&#xff1a; 0元素到它自己的距离是0&#xff0c; 只需考虑1到最近的0是多少距离。 BFS. 先把元素1处的距离更新为无穷大。 0的位置装入queue。 从每个0出发&#xff0c;走上下左右4个方向&…

Redis的8种数据结构和应用场景介绍,面试题答案

面试原题&#xff1a;你用过Redis哪些数据结构&#xff1f;&#xff08;网易一面 2023&#xff09;(面试题来自牛客网) 参考答案 后面有 详细答案解析&#xff0c;帮助更快记忆~ 参考答案共652字符&#xff0c;阅读约需1分8秒&#xff1b;全文共8694字符&#xff0c;阅读约需…

vscode打开nvue,vue3语法文件爆红

由于之前一直是用的vue2&#xff0c;也没用过nvue文件&#xff0c;这次下了hbulider的vue3云模板练手&#xff0c;图快直接在vscode的设置里的关联语言加上了 "files.associations": {// 文件关联语言的优先级配置"*.vue": "vue","*.nvue&…

解决多模块开发中的问题(聚合继承)

&#x1f40c;个人主页&#xff1a; &#x1f40c; 叶落闲庭 &#x1f4a8;我的专栏&#xff1a;&#x1f4a8; c语言 数据结构 javaweb 石可破也&#xff0c;而不可夺坚&#xff1b;丹可磨也&#xff0c;而不可夺赤。 Maven 一、聚合1.1创建Maven模块&#xff0c;设置打包类型…

特斯拉Cybertruck卡车实物照流出,今年 9月可交付?配置报价待定

根据最新的消息&#xff0c;特斯拉的Cybertruck目前正在冰岛的一座冰川上进行路测&#xff0c;并且据称特斯拉的团队还在利用这辆车拍摄相关的广告海报。这进一步表明特斯拉已经进入了Cybertruck的最后测试阶段&#xff0c;并且准备在今年9月底开始交付。然而&#xff0c;有外媒…

MyBatis的基本入门及Idea搭建MyBatis坏境且如何一步骤实现增删改查(CRUD)---详细介绍

一&#xff0c;MaBatis是什么&#xff1f; 首先是一个开源的Java持久化框架&#xff0c;它可以帮助开发人员简化数据库访问的过程并提供了一种将SQL语句与Java代码进行解耦的方式&#xff0c;使得开发人员可以更加灵活地进行数据库操作。 1.1 Mabatis 受欢迎的点 MyBatis不仅是…

使用 OpenTelemetry 构建可观测性 04 - 收集器

在之前的博文中&#xff0c;我们讨论了如何使用 SDK 和链路追踪生产者来导出进程中的遥测数据。尽管有多种类型的导出器可供选择&#xff0c;但其中一个常见的目标是将数据导出到 OpenTelemetry Collector。本篇文章将深入探讨收集器以及如何使用它。 选 OTel Collector 还是…

激活函数总结(十五):振荡系列激活函数补充(SQU、NCU、DSU、SSU)

激活函数总结&#xff08;十五&#xff09;&#xff1a;激活函数补充 1 引言2 激活函数2.1 Shifted Quadratic Unit (SQU) 激活函数2.2 Non-Monotonic Cubic Unit (NCU) 激活函数2.3 Decaying Sine Unit (DSU) 激活函数2.4 Shifted Sinc Unit (SSU) 激活函数 3. 总结 1 引言 在…

豪越科技受邀出席2023中国算力大会

2023年8月17日-8月20日&#xff0c;“算汇银川 数创未来”创新中国行走进银川暨2023中国算力大会在银川中关村创新中心召开。政府领导、行业领袖、专家学者、以及大型科技企业负责人齐聚大会现场&#xff0c;围绕算力基础设施建设、创新应用和产业发展成果等方面开展广泛交流与…

NSF拨款3800万美元让更多机构参与量子科学与工程

近日&#xff0c;美国国家科学基金会&#xff08;National Science Foundation&#xff0c;NSF&#xff09;宣布对“量子信息科学与工程扩展能力”&#xff08;Expanding Capacity in Quantum Information Science and Engineering&#xff0c;ExpandQISE&#xff09;计划拨款3…

好消息,微信又有免费提现活动了

​明天就是一年一度的七夕佳节&#xff0c;微信推出了「浪漫七夕&#xff0c;情寄明灯」活动&#xff0c;凡参与活动都可获得免费提现券等奖励。 01 活动时间 8 月 21 日 10 点至 8 月 24 日 24 点。 02 如何参与 活动入口&#xff1a; 在「微信支付有优惠」小程序专属入口…

解读2023年上半年财报:继续押注儿童业务的361°,有着怎样的野心?

“足球热”的风还是吹到了青少年身边&#xff0c;近日&#xff0c;济南历城二中女足问鼎2023世界中学生足球锦标赛女子组冠军&#xff0c;中国球队时隔16年再次获得世界中学生足球锦标赛冠军&#xff0c;点燃了不少足球爱好者的热情。 少儿体育热之下&#xff0c;与之相关的运…

Php“牵手”淘宝商品销量数据采集方法,淘宝API接口申请指南

淘宝天猫商品销量接口 API 是开放平台提供的一种 API 接口&#xff0c;它可以帮助开发者获取商品的详细信息&#xff0c;包括商品的标题、描述、图片&#xff0c;月销量&#xff0c;总销量等信息。在电商平台的开发中&#xff0c;销量接口API是非常常用的 API&#xff0c;因此本…