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

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

 更新時(shí)間:2022年11月18日 14:43:02   作者:7_erQ  
這篇文章主要為大家介紹了Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

在平常的Unity開(kāi)發(fā)過(guò)程中,可能會(huì)遇到如:

1.使用Debug.Log替代輸出異常信息;

2.調(diào)試代碼時(shí),源代碼在try{}代碼塊內(nèi)有較多或深層的調(diào)用;

3.想在輸出的Log中提示或是引導(dǎo)其他開(kāi)發(fā)人員打開(kāi)指定的腳本等情景。

在上述情景中,Debug.Log輸出的Log一般都是不帶點(diǎn)擊跳轉(zhuǎn)功能的,使得我們需要在長(zhǎng)長(zhǎng)的Log中尋找目標(biāo)文件,然后再對(duì)照著文件名,方法名在IDE中點(diǎn)開(kāi),并不是很方便。

不帶點(diǎn)擊跳轉(zhuǎn)的Log

public static void TestFunc() {
    try {
        TestCall_1("", 0);
    } catch (System.Exception e) {
        // 平時(shí)使用Debug輸出的Log是不帶點(diǎn)擊跳轉(zhuǎn)的
        Debug.LogError(e.ToString());
    }
}

輸出結(jié)果

如果在try catch中做了很復(fù)雜的操作,這樣的Log將會(huì)又長(zhǎng)又亂

后來(lái)經(jīng)過(guò)反復(fù)比對(duì)帶點(diǎn)擊跳轉(zhuǎn)和不帶點(diǎn)擊跳轉(zhuǎn)的兩行Log,發(fā)現(xiàn)了Unity識(shí)別這種跳轉(zhuǎn)路徑的基本格式為:()[空格](at X:0),(空格不能省略)。因此只要把要跳轉(zhuǎn)的腳本路徑照這個(gè)格式封裝就能實(shí)現(xiàn)高亮點(diǎn)擊跳轉(zhuǎn)了。下面是演示代碼。

帶點(diǎn)擊跳轉(zhuǎn)的Log

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
???????public class TestEditor {
    [MenuItem("測(cè)試/打印異常")]
    public static void TestFunc() {
        try {
            TestCall_1("", 0);
        } catch (System.Exception e) {
            string output = "使Log帶跳轉(zhuǎn)功能 \n";
            // 1.這是能被識(shí)別為帶點(diǎn)擊跳轉(zhuǎn)的格式:() (at XXX:0)
            output += "() (at X:0) \n";
             // 2.可以是絕對(duì)路徑,但格式好像只能是.cs文件,若.cs文件不存在,則會(huì)在IDE中打開(kāi)一個(gè)新文檔
? ? ? ? ? ? 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é)果

帶點(diǎn)擊跳轉(zhuǎn)的Log

這樣就可以直接點(diǎn)開(kāi)定位到目標(biāo)文件和方法了 作者:7_erQ https://www.bilibili.com/read/cv15464238 出處:bilibili

以上就是Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解的詳細(xì)內(nèi)容,更多關(guān)于Unity輸出點(diǎn)擊跳轉(zhuǎn)Log的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論