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):默认独占一行,不能并列显示,能够设 ...
随机推荐
- linux 文件系统管理三部曲之一:磁盘分区
硬盘和主板的连接的地方:叫接口,硬盘的接口类型: iops:i/o per second:每秒的读写次数. IDE(ata):并口(数据并行传输),理论最大传输 133MB/S:iops:100次 S ...
- Spark学习之路 (二十八)分布式图计算系统[转]
引言 在了解GraphX之前,需要先了解关于通用的分布式图计算框架的两个常见问题:图存储模式和图计算模式. 图存储模式 巨型图的存储总体上有边分割和点分割两种存储方式.2013年,GraphLab2. ...
- 使用Python爬虫整理小说网资源-自学
第一次接触python,原本C语言的习惯使得我还不是很适应python的语法风格.希望读者能够给出建议. 相关的入门指导来自以下的网址:https://blog.csdn.net/c406495762 ...
- Easy_language
http://www.guosen.com.cn/gxzq/tradestation/help/operate/operate06.html power language https://seekin ...
- Firefox下载.net服务器文件时中文乱码
ASP.NET中产生一个Excel报表,供浏览器下载.显然,下载时经常有汉字报表名称.针对当前的两个主流的浏览器IE和Firefox,似乎没有兼容的解决办法.网上搜索了一些解决方法,但在Firefox ...
- Ajax基础原理与应用
Ajax函数封装ajax.js // Get / Post // 参数 get post // 是否异步 // 如何处理响应数据 // URL // var handleResponse = func ...
- [Agc005D/At2060] Minimum Sum - 单调栈
鉴于早上那题让我怀疑单调栈白学,特意来复习下单调栈 题意 考虑按照每个元素对答案的贡献来统计,那么我们只需要找到每个元素左边右边第一个比它小的就可 这题给的又是排列,简直不能再良心 #include ...
- keepalived高可用工具
1.准备俩台虚拟机,一台主机,一台备机 我这里模拟的是 主机ip: 192.168.42.66 masternginx 备机ip: 192.168.42.77 slavenginx 虚拟ip: 192 ...
- 走进电影院观看VTK
VTK影院模型: 从这个模型去介绍VTK的应用,整个电影院就是VTK的显示窗口(vtkRenderWindow),舞台就是VTK的渲染场景(vtkRenderer),场景中有不同的演员就是VTK的各种 ...
- BOM笔记
目录 BOM (浏览器对象模型) 简介 window对象 子对象 history 子对象 lacation 子对象 navigator 子对象 screen 子对象 frames BOM (浏览器对象 ...