ConsoleWindow中的双击日志定位
很多项目都有自己重写Debug.Log的习惯,难免会遇到在Unity的Console窗口中双击日志, 但是没法直接跳转到想要看到的代码那一行的时候,解决办法有以下2种:
- 将自己封装的日志类制作成DLL再导入到Unity使用,但是有时候想修改日志类的代码却比较麻烦了~~
- 使用本文提供的LogEditor类来自动定位!。!下面来详细说明下该类吧~~
使用方法:
- 将LogEditor类的脚本放到名字为Editor的目录下(如果没有Editor目录就新建一个吧)
- 在这里修改和添加自己封装过的日志类(路径+类型),支持添加多个封装的日志类
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine; namespace shaco
{
public static class LogEditor
{
private class LogEditorConfig
{
public string logScriptPath = "";
public string logTypeName = "";
public int instanceID = ; public LogEditorConfig(string logScriptPath, System.Type logType)
{
this.logScriptPath = logScriptPath;
this.logTypeName = logType.FullName;
}
} //Add your custom Log class here
private static LogEditorConfig[] _logEditorConfig = new LogEditorConfig[]
{
new LogEditorConfig("Assets/shaco/Base/Scripts/Unity/Debug/Log.cs", typeof(shaco.Log)),
new LogEditorConfig("Assets/shaco/Base/Scripts/CSharp/Debug/Log.cs", typeof(shaco.Base.Log))
}; [UnityEditor.Callbacks.OnOpenAssetAttribute(-)]
private static bool OnOpenAsset(int instanceID, int line)
{
for (int i = _logEditorConfig.Length - ; i >= ; --i)
{
var configTmp = _logEditorConfig[i];
UpdateLogInstanceID(configTmp);
if (instanceID == configTmp.instanceID)
{
var statckTrack = GetStackTrace();
if (!string.IsNullOrEmpty(statckTrack))
{
var fileNames = statckTrack.Split('\n');
var fileName = GetCurrentFullFileName(fileNames);
var fileLine = LogFileNameToFileLine(fileName);
fileName = GetRealFileName(fileName); AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(fileName), fileLine);
return true;
}
break;
}
} return false;
} private static string GetStackTrace()
{
var consoleWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow");
var fieldInfo = consoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);
var consoleWindowInstance = fieldInfo.GetValue(null); if (null != consoleWindowInstance)
{
if ((object)EditorWindow.focusedWindow == consoleWindowInstance)
{
// Get ListViewState in ConsoleWindow
// var listViewStateType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ListViewState");
// fieldInfo = consoleWindowType.GetField("m_ListView", BindingFlags.Instance | BindingFlags.NonPublic);
// var listView = fieldInfo.GetValue(consoleWindowInstance); // Get row in listViewState
// fieldInfo = listViewStateType.GetField("row", BindingFlags.Instance | BindingFlags.Public);
// int row = (int)fieldInfo.GetValue(listView); // Get m_ActiveText in ConsoleWindow
fieldInfo = consoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);
string activeText = fieldInfo.GetValue(consoleWindowInstance).ToString(); return activeText;
}
}
return "";
} private static void UpdateLogInstanceID(LogEditorConfig config)
{
if (config.instanceID > )
{
return;
} var assetLoadTmp = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(config.logScriptPath);
if (null == assetLoadTmp)
{
throw new System.Exception("not find asset by path=" + config.logScriptPath);
}
config.instanceID = assetLoadTmp.GetInstanceID();
} private static string GetCurrentFullFileName(string[] fileNames)
{
string retValue = "";
int findIndex = -; for (int i = fileNames.Length - ; i >= ; --i)
{
bool isCustomLog = false;
for (int j = _logEditorConfig.Length - ; j >= ; --j)
{
if (fileNames[i].Contains(_logEditorConfig[j].logTypeName))
{
isCustomLog = true;
break;
}
}
if (isCustomLog)
{
findIndex = i;
break;
}
} if (findIndex >= && findIndex < fileNames.Length - )
{
retValue = fileNames[findIndex + ];
} return retValue;
} private static string GetRealFileName(string fileName)
{
int indexStart = fileName.IndexOf("(at ") + "(at ".Length;
int indexEnd = ParseFileLineStartIndex(fileName) - ; fileName = fileName.Substring(indexStart, indexEnd - indexStart);
return fileName;
} private static int LogFileNameToFileLine(string fileName)
{
int findIndex = ParseFileLineStartIndex(fileName);
string stringParseLine = "";
for (int i = findIndex; i < fileName.Length; ++i)
{
var charCheck = fileName[i];
if (!IsNumber(charCheck))
{
break;
}
else
{
stringParseLine += charCheck;
}
} return int.Parse(stringParseLine);
} private static int ParseFileLineStartIndex(string fileName)
{
int retValue = -;
for (int i = fileName.Length - ; i >= ; --i)
{
var charCheck = fileName[i];
bool isNumber = IsNumber(charCheck);
if (isNumber)
{
retValue = i;
}
else
{
if (retValue != -)
{
break;
}
}
}
return retValue;
} private static bool IsNumber(char c)
{
return c >= '' && c <= '';
}
}
}
本文为转载, 原文链接:https://blog.csdn.net/l449612236/article/details/76087616
ConsoleWindow中的双击日志定位的更多相关文章
- SQL Server中的事务日志管理(9/9):监控事务日志
当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...
- 使用SeasLog打造PHP项目中的高性能日志组件(一)
云智慧(北京)科技有限公司 高驰涛 什么是SeasLog SeasLog是一个C语言编写的PHP扩展,提供一组规范标准的功能函数,在PHP项目中方便.规范.高效地写日志,以及快速地读取和查询日志. 为 ...
- 把嵌入在eclipse中的tomcat日志分离出来
现象 不知道从哪个版本的tomcat开始,windows版本的tomcat不再包含{tomcat_home}\logs\catalina.out这个文件,eclipse中配置好tomcat服务器之后, ...
- C#中使用打印日志
在日常的工作中经常需要日志,这样能够很容易定位到代码中的一些错误,.Net中有自带的日志接口.并没有仔细去研究,这里是我自己写的日志接口,记录下来以便以后用到,根据时间打印相关的日志文件,代码如下: ...
- 在windows中:双击运行Python程序、后台运行Python程序
在windows中:双击运行Python程序.后台运行Python程序 安装Python解释器的windows环境,如果双击运行*.py的文件,会闪退.怎样避免闪退呢? 我们用python的日志输出程 ...
- SQL Server中的事务日志管理的阶梯,级别1:事务日志概述
SQL Server中的事务日志管理的阶梯,级别1:事务日志概述 翻译:刘琼滨 谢雪妮 许雅莉 赖慧芳 级别1:事务日志概述 事务日志是一个文件,其中SQL服务器存储了所有与日志文件关联的数据库执行的 ...
- Xcode8中处理打印日志的配置
Xcode8中处理打印日志的配置
- css中的大小、定位、轮廓相关属性
css中的大小.定位.轮廓相关属性 1.通过height.width属性控制组件大小 height:高度,可以设置任何有效的距离值: width:宽度,可以设置任何有效的属性值: max-height ...
- CSS中的浮动和定位
在了解CSS中的浮动和定位之前有必要先了解清楚标准流和脱离标准流的特性 标准流的默认特性 1.分行.块级元素,并且能够dispay转换. 2.块级元素(block):默认独占一行,不能并列显示,能够设 ...
随机推荐
- shadow文件中密码的加密方式
1) 查看shadow文件的内容 cat /etc/shadow 可以得到shadow文件的内容,限于篇幅,我们举例说明: root:$1$Bg1H/4mz$X89TqH7tpi9dX1B9j5YsF ...
- 使用Python库paramiko登录远程设备
前言 手动下载paramiko库的安装包.在PyPi库中查找即可,但是不到是我的电脑问题还是网络问题,2.0.0以上版本我都安装不了,因此我自己是安装的paramiko 1.17.0版本,此版本经过测 ...
- html里面用Jquery移除tr元素后,滚动条会回到顶部问题处理
问题如下图,删除一行后,滚动条会自动回到顶部,即使先把滚动条禁止也还是会回到顶部 参考这个 https://bbs.csdn.net/topics/392233437 发现确实自己的按钮事件写在了a标 ...
- Jquery实现挂号平台首页源码
带二级导航.轮播海报.二级联动.搜索功能.Tab选项卡 按照国际惯例先放图 index.html <!DOCTYPE html> <html lang="zh-cn&quo ...
- ubuntu18.04 安装与卸载 php7.2
安装: 如果之前有其他版本PHP,在这边禁用掉 1 sudo a2dismod php5 再来安装做准备 1234 sudo apt-get install software-properties-c ...
- 日常问题--解决 ‘Could not fetch URL https://pypi.python.org’的问题
难题描述: 解决方法: 使用命令python -m pip install Scrapy --trusted-host=pypi.python.org --trusted-host=pypi.org ...
- laravle中orm简单的增删改查
友情提示请在有laravel基础的情况下观看文章 1.数据库信息(user表) CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...
- 1、腾讯云搭建Hadoop3集群
1主机名和IP配置 1.1主机名 1.首先使用root用户名和root密码分别登录三台服务器 2.分别在三台虚拟机上执行命令: hostnamectl set-hostname node1 hostn ...
- unity ui中使用onmouseover
unity ui中鼠标移进或者移出的触发方式与2d.3d的不同,2d.3d物体使用的是onmouseover,ui使用的是OnPointerEnter.需要实现以下两个接口. public class ...
- Subway POJ - 2502 spfa
#include<cstdio> #include<cmath> #include<cstring> #include<cstring> #includ ...