Unity输出带点击跳转功能的Log实现技巧详解

这篇文章主要为大家介绍了Unity输出带点击跳转功能的Log实现技巧详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

正文

在平常的Unity开发过程中,可能会遇到如:

1.使用Debug.Log替代输出异常信息;

2.调试代码时,源代码在try{}代码块内有较多或深层的调用;

3.想在输出的Log中提示或是引导其他开发人员打开指定的脚本等情景。

在上述情景中,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将会又长又乱

后来经过反复比对带点击跳转和不带点击跳转的两行Log,发现了Unity识别这种跳转路径的基本格式为:()[空格](at X:0),(空格不能省略)。因此只要把要跳转的脚本路径照这个格式封装就能实现高亮点击跳转了。下面是演示代码。

带点击跳转的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实现技巧详解的详细内容,更多关于Unity输出点击跳转Log的资料请关注0133技术站其它相关文章!

以上就是Unity输出带点击跳转功能的Log实现技巧详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程