广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Unity输出带点击跳转功能的Log实现技巧详解
  • 303
分享到

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

Unity输出点击跳转LogUnityLog 2022-11-21 22:11:59 303人浏览 独家记忆
摘要

目录正文不带点击跳转的Log带点击跳转的Log正文 在平常的Unity开发过程中,可能会遇到如: 1.使用Debug.Log替代输出异常信息; 2.调试代码时,源代码在try{}代码

正文

在平常的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的资料请关注编程网其它相关文章!

--结束END--

本文标题: Unity输出带点击跳转功能的Log实现技巧详解

本文链接: https://www.lsjlt.com/news/173354.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作