Unity 扩展Log日志

Unity 扩展Log 类输出自定义日志

前言

Unity 里面在进行Android Hololens ios等平台进行测试的时候,如果不借助别的工具,没办法看到控制台输出的日志,给测试带来很多麻烦,如果我们能够将输出的Log输出到UI上就方便我们自己的调试,接下来就是做这么一件事情。
使用反编译软件打开UnityEngine.CoreModule.dll文件,会发现Debug类里面的方法和属性如下图:
在这里插入图片描述
在这里插入图片描述
它是使用unityLogger进行输出,unityLogger=>s_Logger,然而s_Logger是一个接口,也就是说我们只需要用反射去自定义这个s_Logger就能使用自己定义的Log输出了。

首先先准备一个实现ILogger接口的类ILoggerExtension。要输出的信息全部在这些实现的接口里面。可以统一输出,代码如下。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public  class ILoggerExtension : ILogger
{

    public static string logString;
    public ILogHandler logHandler { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public bool logEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public LogType filterLogType { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

    public bool IsLogTypeAllowed(LogType logType)
    {
        throw new NotImplementedException();
    }

    public void Log(LogType logType, object message)
    {
        logString = message.ToString();
    }

    public void Log(LogType logType, object message, UnityEngine.Object context)
    {
        logString = message.ToString();

    }

    public void Log(LogType logType, string tag, object message)
    {
        logString = message.ToString();

    }

    public void Log(LogType logType, string tag, object message, UnityEngine.Object context)
    {
        logString = message.ToString();

    }

    public void Log(object message)
    {
        logString = message.ToString();

    }

    public void Log(string tag, object message)
    {
        logString = message.ToString();

    }

    public void Log(string tag, object message, UnityEngine.Object context)
    {
        logString = message.ToString();

    }

    public void LogError(string tag, object message)
    {
        logString = message.ToString();

    }

    public void LogError(string tag, object message, UnityEngine.Object context)
    {
                logString = message.ToString();

    }

    public void LogException(Exception exception)
    {
        throw new NotImplementedException();
    }

    public void LogException(Exception exception, UnityEngine.Object context)
    {
        throw new NotImplementedException();
    }

    public void LogFormat(LogType logType, string format, params object[] args)
    {
        throw new NotImplementedException();
    }

    public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    {
        throw new NotImplementedException();
    }

    public void LogWarning(string tag, object message)
    {
        throw new NotImplementedException();
    }

    public void LogWarning(string tag, object message, UnityEngine.Object context)
    {
        throw new NotImplementedException();
    }

    
}

然后使用反射去设置自定义的s_Logger,也是需要定义一个类LogExtension,代码如下
下面展示一些 内联代码片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
public static class LogExtension
{
    public static void SetILogger() {
        if (Debug.unityLogger is ILoggerExtension) return;
        FieldInfo fieldInfo = typeof(Debug).GetField("s_Logger", BindingFlags.Static | BindingFlags.NonPublic);
        if (fieldInfo!=null) fieldInfo.SetValue(null, new ILoggerExtension()); 
    }

}

后面就可以进行测试了,定义一个测试类Log,我们就可以将自定义的日志输出到text了,就不用关心在哪里调用输出,直接使用Unity Debug类就行了。
下面展示一些 内联代码片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Log : MonoBehaviour
{
    public Text text;
    private void Awake()
    {
        LogExtension.SetILogger();
    }

    void Update()
    {
        text.text = ILoggerExtension.logString;

        if (Input.GetKeyDown(KeyCode.Space)) {
            Debug.Log("按下Log"+Time.deltaTime);
        
        }
    }
}

项目参考链接:https://download.csdn.net/download/qq_33547099/13683493

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33547099/article/details/111149562
今日推荐