Skip to content

Instantly share code, notes, and snippets.

@aefreedman
Created August 28, 2023 16:03
Show Gist options
  • Save aefreedman/f6f8b8218b6c187f43beb2f5f46cb4bc to your computer and use it in GitHub Desktop.
Save aefreedman/f6f8b8218b6c187f43beb2f5f46cb4bc to your computer and use it in GitHub Desktop.
GUILayout debug log for Unity
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