Created
August 28, 2023 16:03
-
-
Save aefreedman/f6f8b8218b6c187f43beb2f5f46cb4bc to your computer and use it in GitHub Desktop.
GUILayout debug log for Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
/// <summary> | |
/// From https://stackoverflow.com/questions/67704820/how-do-i-print-unitys-debug-log-to-the-screen-gui | |
/// </summary> | |
public class VisualDebugLog : MonoBehaviour | |
{ | |
private readonly Queue _myLogQueue = new(); | |
private const uint Qsize = 15; // number of messages to keep | |
private void Start() | |
{ | |
Debug.Log("Started up logging."); | |
} | |
private void OnEnable() | |
{ | |
Application.logMessageReceived += HandleLog; | |
} | |
private void OnDisable() | |
{ | |
Application.logMessageReceived -= HandleLog; | |
} | |
private void HandleLog(string logString, string stackTrace, LogType type) | |
{ | |
_myLogQueue.Enqueue("[" + type + "] : " + logString); | |
if (type == LogType.Exception) | |
_myLogQueue.Enqueue(stackTrace); | |
while (_myLogQueue.Count > Qsize) | |
_myLogQueue.Dequeue(); | |
} | |
private void OnGUI() | |
{ | |
GUILayout.BeginArea(new Rect(Screen.width - 400, 0, 400, Screen.height)); | |
GUILayout.Label("\n" + string.Join("\n", _myLogQueue.ToArray())); | |
GUILayout.EndArea(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment