Unity輸出帶點擊跳轉功能的Log實現(xiàn)技巧詳解
正文
在平常的Unity開發(fā)過程中,可能會遇到如:
1.使用Debug.Log替代輸出異常信息;
2.調試代碼時,源代碼在try{}代碼塊內有較多或深層的調用;
3.想在輸出的Log中提示或是引導其他開發(fā)人員打開指定的腳本等情景。
在上述情景中,Debug.Log輸出的Log一般都是不帶點擊跳轉功能的,使得我們需要在長長的Log中尋找目標文件,然后再對照著文件名,方法名在IDE中點開,并不是很方便。
不帶點擊跳轉的Log
public static void TestFunc() { try { TestCall_1("", 0); } catch (System.Exception e) { // 平時使用Debug輸出的Log是不帶點擊跳轉的 Debug.LogError(e.ToString()); } }
輸出結果
如果在try catch中做了很復雜的操作,這樣的Log將會又長又亂
后來經(jīng)過反復比對帶點擊跳轉和不帶點擊跳轉的兩行Log,發(fā)現(xiàn)了Unity識別這種跳轉路徑的基本格式為:()[空格](at X:0),(空格不能省略)。因此只要把要跳轉的腳本路徑照這個格式封裝就能實現(xiàn)高亮點擊跳轉了。下面是演示代碼。
帶點擊跳轉的Log
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.Text.RegularExpressions; ???????public class TestEditor { [MenuItem("測試/打印異常")] public static void TestFunc() { try { TestCall_1("", 0); } catch (System.Exception e) { string output = "使Log帶跳轉功能 \n"; // 1.這是能被識別為帶點擊跳轉的格式:() (at XXX:0) output += "() (at X:0) \n"; // 2.可以是絕對路徑,但格式好像只能是.cs文件,若.cs文件不存在,則會在IDE中打開一個新文檔 ? ? ? ? ? ? output += "() (at " + @"C:/Users/7_erQ/Desktop/test.cs" + ":0) \n"; ? ? ? ? ? ? // 3.用正則替換原Log為 XXX.xxx() (at XXX.cs:nnn)的格式,就能使Log帶跳轉功能了 ? ? ? ? ? ? // 原Log: ?at TestEditor.TestCall_4 (System.String arg1, System.Int32 arg2) [0x00001] in F:\WorkStation\CodeSpace\UnityProjects\AssetLinkMapDemo\Assets\Temp\Editor\TestEditor.cs:41 ? ? ? ? ? ? // 替換后: ?at TestEditor.TestCall_4 (System.String arg1, System.Int32 arg2) (at F:\WorkStation\CodeSpace\UnityProjects\AssetLinkMapDemo\Assets\Temp\Editor\TestEditor.cs:42) ? ? ? ? ? ? Regex reg = new Regex(@"\)\s\[0x[0-9,a-f]*\]\sin\s(.*:[0-9]*)\s"); ? ? ? ? ? ? output += reg.Replace(e.ToString(), ") (at $1) "); ? ? ? ? ? ? Debug.LogError(output); ? ? ? ? } ? ? } ? ? public static void TestCall_1(string arg1, int arg2) { ? ? ? ? TestCall_2(arg1, arg2); ? ? } ? ? public static void TestCall_2(string arg1, int arg2) { ? ? ? ? TestCall_3(arg1, arg2); ? ? } ? ? public static void TestCall_3(string arg1, int arg2) { ? ? ? ? TestCall_4(arg1, arg2); ? ? } ? ? public static void TestCall_4(string arg1, int arg2) { ? ? ? ? throw new System.Exception(); ? ? } }
輸出結果
帶點擊跳轉的Log
這樣就可以直接點開定位到目標文件和方法了 作者:7_erQ https://www.bilibili.com/read/cv15464238 出處:bilibili
以上就是Unity輸出帶點擊跳轉功能的Log實現(xiàn)技巧詳解的詳細內容,更多關于Unity輸出點擊跳轉Log的資料請關注腳本之家其它相關文章!
相關文章
C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的方式
這篇文章主要給大家介紹了關于C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08C#使用FileSystemWatcher控件實現(xiàn)的文件監(jiān)控功能示例
這篇文章主要介紹了C#使用FileSystemWatcher控件實現(xiàn)的文件監(jiān)控功能,結合實例形式分析了C# FileSystemWatcher組件的功能及監(jiān)控文件更改情況的具體使用技巧,需要的朋友可以參考下2017-08-08C#使用CancellationTokenSource 取消 Task的方法
因為涉及到了日常經(jīng)常會碰到的取消任務操作,本文主要介紹了C#使用CancellationTokenSource 取消 Task,文中通過代碼介紹的非常詳細,感興趣的可以了解一下2022-02-02