使用LogKit.cs后双击log定位行号问题解决方法
clksaaa opened this issue · 3 comments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
/*
-
原文地址 https://zhuanlan.zhihu.com/p/92291084
*/
public static class OpenAssetLogLine
{
private static bool m_hasForceMono = false;
// 处理asset打开的callback函数
[UnityEditor.Callbacks.OnOpenAssetAttribute(-1)]
static bool OnOpenAsset(int instance, int line)
{
if (m_hasForceMono) return false;// 自定义函数,用来获取log中的stacktrace,定义在后面。 string stack_trace = GetStackTrace(); // 通过stacktrace来定位是否是自定义的log,log中有LogKit/LogKit.cs,很好识别 if (!string.IsNullOrEmpty(stack_trace) && stack_trace.Contains("LogKit/LogKit.cs")) { // 正则匹配at xxx,在第几行 Match matches = Regex.Match(stack_trace, @"\(at (.+)\)", RegexOptions.IgnoreCase); string pathline = ""; while (matches.Success) { pathline = matches.Groups[1].Value; // 找到不是我们自定义log文件的那行,重新整理文件路径,手动打开 if (!pathline.Contains("LogKit/LogKit.cs")&&!string.IsNullOrEmpty(pathline)) { int split_index = pathline.LastIndexOf(":"); string path = pathline.Substring(0, split_index); line = Convert.ToInt32(pathline.Substring(split_index + 1)); m_hasForceMono = true; //方式一 AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path), line); m_hasForceMono = false; //方式二 //string fullpath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("Assets")); // fullpath = fullpath + path; // UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(fullpath.Replace('/', '\\'), line); return true; } matches = matches.NextMatch(); } return true; } return false;}
static string GetStackTrace()
{
// 找到类UnityEditor.ConsoleWindow
var type_console_window = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow");
// 找到UnityEditor.ConsoleWindow中的成员ms_ConsoleWindow
var filedInfo = type_console_window.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);
// 获取ms_ConsoleWindow的值
var ConsoleWindowInstance = filedInfo.GetValue(null);
if (ConsoleWindowInstance != null)
{
if ((object)EditorWindow.focusedWindow == ConsoleWindowInstance)
{
// 找到类UnityEditor.ConsoleWindow中的成员m_ActiveText
filedInfo = type_console_window.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);
string activeText = filedInfo.GetValue(ConsoleWindowInstance).ToString();
return activeText;
}
}
return null;
}
}
非常感谢,我尽快会用这种方法写到 QFramework 中并把你列到贡献名单中。
赞 qf越来越好了
不好意思 时隔一年才有空加到 qf 本体中,已经添加,感谢反馈。