Assert Store上有一个Editor Console Pro,功能很是全面,百度也能搜到破解。若是有须要建议使用,不要再造车轮ide
起初由于自带Console功能太弱,有很多能够提高空间。因而尝试本身写,但是写到后面发现上面那个工具。。大体原理明白以后发上来分享一下。工具
=======================================spa
读到Log信息,有两个方法线程
1.Application.RegisterLogCallbackdebug
惋惜是运行时用的,Editor下须要建立一个GameObject绑上运行时脚本,还有一个很严重的问题,就是会占用其余的回调注册,或者被占用而读不到Log。3d
2.调用内部类LogEntries指针
在看EditorConsolePro源码时发现它这么用,很奇怪这个类Unity官方没彻底开放出来,在UnityEditorInternal.LogEtries下,只能经过反射调用。Unity官方论坛的资料也不多code
-------------------------------------------------------blog
在尝试第一种方法无果以后,选用第二种方法。接口
ILSpy里稍微看了下
GetCount()能够直接获得Log的总数
bool GetEntryInternal(int,LogEntry) 能够获得详细的Title和stack信息。
int StartGettingEntries()和void EndGettingEntries()在取值的时候须要调用两个方法包围取值代码,不然会报指针错误。
SetConsoleFlag(int,bool)能够屏蔽Warning,Error之类的,和自带Console同样。
Clear()也是自带Console的Clear。
取值代码以下
string GetSourceText(int row) { var LogEntriesType = typeof(EditorWindow).Assembly.GetType("UnityEditorInternal.LogEntries"); var startGettingEntriesMethod = LogEntriesType.GetMethod("StartGettingEntries", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); var endGettingEntriesMethod = LogEntriesType.GetMethod("EndGettingEntries", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); startGettingEntriesMethod.Invoke(null, new object[0]); var GetEntryInternalMethod = LogEntriesType.GetMethod("GetEntryInternal", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); var logEntryType = typeof(EditorWindow).Assembly.GetType("UnityEditorInternal.LogEntry"); var logEntry = Activator.CreateInstance(logEntryType); //Get detail debug info. GetEntryInternalMethod.Invoke(null, new object[2] { row, logEntry }); //More info please search "UnityEditorInternal.LogEntry" class of ILSPY. var fieldInfo = logEntryType.GetField("condition", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var result = fieldInfo.GetValue(logEntry).ToString(); endGettingEntriesMethod.Invoke(null, new object[0]); return result; } int GetCount() { var debugType = typeof(EditorWindow).Assembly.GetType("UnityEditorInternal.LogEntries"); var methodInfo = debugType.GetMethod("GetCount", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); return (int)methodInfo.Invoke(null, new object[0]); }
调用GetEntryInternal取到的Log会返回LogEntry class结构,其中condition是栈跟踪信息。
而后遇到第二个问题,读到的Log信息始终都是最末输出的那个。
查了一下发现是用GetStatusTest()来读的Title,只能读到最后一行,彷佛Unity内部输出Debug信息是在另外一个线程里,才致使这个问题。
因而对每次获得的Count和上一次的Count值对比从新遍历,以输出全部的信息。
还须要加Count数的改变判断,不会像注册Log回调那样直接给你每条Log
点击具体Log跳转到IDE指定行数,调用这个接口
InternalEditorUtility.OpenFileAtLineExternal(Path, Line);
这样的话,能够作到对Console过滤,加标签。
具体就写到这里