1. 程式人生 > 其它 >Unity 擴充套件Log日誌

Unity 擴充套件Log日誌

技術標籤:Unity

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