Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解
正文
在平常的Unity開發(fā)過程中,可能會遇到如:
1.使用Debug.Log替代輸出異常信息;
2.調(diào)試代碼時,源代碼在try{}代碼塊內(nèi)有較多或深層的調(diào)用;
3.想在輸出的Log中提示或是引導(dǎo)其他開發(fā)人員打開指定的腳本等情景。
在上述情景中,Debug.Log輸出的Log一般都是不帶點擊跳轉(zhuǎn)功能的,使得我們需要在長長的Log中尋找目標文件,然后再對照著文件名,方法名在IDE中點開,并不是很方便。
不帶點擊跳轉(zhuǎn)的Log
public static void TestFunc() {
try {
TestCall_1("", 0);
} catch (System.Exception e) {
// 平時使用Debug輸出的Log是不帶點擊跳轉(zhuǎn)的
Debug.LogError(e.ToString());
}
}輸出結(jié)果

如果在try catch中做了很復(fù)雜的操作,這樣的Log將會又長又亂
后來經(jīng)過反復(fù)比對帶點擊跳轉(zhuǎn)和不帶點擊跳轉(zhuǎn)的兩行Log,發(fā)現(xiàn)了Unity識別這種跳轉(zhuǎn)路徑的基本格式為:()[空格](at X:0),(空格不能省略)。因此只要把要跳轉(zhuǎn)的腳本路徑照這個格式封裝就能實現(xiàn)高亮點擊跳轉(zhuǎn)了。下面是演示代碼。
帶點擊跳轉(zhuǎ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帶跳轉(zhuǎn)功能 \n";
// 1.這是能被識別為帶點擊跳轉(zhuǎn)的格式:() (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帶跳轉(zhuǎn)功能了
? ? ? ? ? ? // 原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();
? ? }
}輸出結(jié)果

帶點擊跳轉(zhuǎn)的Log
這樣就可以直接點開定位到目標文件和方法了 作者:7_erQ https://www.bilibili.com/read/cv15464238 出處:bilibili
以上就是Unity輸出帶點擊跳轉(zhuǎn)功能的Log實現(xiàn)技巧詳解的詳細內(nèi)容,更多關(guān)于Unity輸出點擊跳轉(zhuǎn)Log的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#快速實現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-02-02
解析在內(nèi)部循環(huán)中Continue外部循環(huán)的使用詳解
本篇文章是對在內(nèi)部循環(huán)中Continue外部循環(huán)的使用進行了詳細的分析介紹,需要的朋友參考下2013-05-05

