欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity輸出帶點擊跳轉功能的Log實現(xiàn)技巧詳解

 更新時間:2022年11月18日 14:43:02   作者:7_erQ  
這篇文章主要為大家介紹了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的資料請關注腳本之家其它相關文章!

相關文章

最新評論