본문 바로가기

Unity/Input

[Unity] Debug.Log를 화면에 보여주기 (GUILayout)

Debug.Log를 화면에 보여주기 (GUILayout)

 

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

public class MyLog : MonoBehaviour
{
    string myLog;
    Queue myLogQueue = new Queue();
    private GUIStyle guiStyle = new GUIStyle();

    void Start()
    {
        guiStyle.fontSize = 20;
        Debug.Log("MyLog Start");
    }

    void OnEnable()
    {
        Application.logMessageReceived += HandleLog;
    }

    void OnDisable()
    {
        Application.logMessageReceived -= HandleLog;
    }

    void HandleLog(string logString, string stackTrace, LogType type)
    {
        myLog = logString;
        string newString = "\n [" + type + "] : " + myLog;
        myLogQueue.Enqueue(newString);
        if (type == LogType.Exception)
        {
            newString = "\n" + stackTrace;
            myLogQueue.Enqueue(newString);
        }
        myLog = string.Empty;
        foreach (string mylog in myLogQueue)
        {
            myLog += mylog;
        }
    }

    
    

    void OnGUI()
    {
        GUILayout.Label(myLog, guiStyle);
    }
}