Skip to content

Instantly share code, notes, and snippets.

@dyguests
Created October 18, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyguests/1a6dfa74e30b2bf60fa77001fb734829 to your computer and use it in GitHub Desktop.
Save dyguests/1a6dfa74e30b2bf60fa77001fb734829 to your computer and use it in GitHub Desktop.
LogGui, Unity log on screen, Screen Log.
using System.Collections.Generic;
using UnityEngine;
namespace Plugins.FanhlCores.Tools
{
public class LogGui : MonoBehaviour
{
private const int MaxChars = 10000;
private static LogGui sInstance;
private readonly Queue<string> logQueue = new();
private string log;
void Awake()
{
if (sInstance == null)
{
sInstance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
return;
}
}
void Start()
{
Debug.Log("LogGui start.");
}
void OnEnable()
{
Application.logMessageReceived += HandleLog;
}
void OnDisable()
{
Application.logMessageReceived -= HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type)
{
logQueue.Enqueue("\n [" + type + "] : " + logString);
if (type == LogType.Exception)
logQueue.Enqueue("\n" + stackTrace);
}
void Update()
{
while (logQueue.Count > 0)
log = logQueue.Dequeue() + log;
if (log.Length > MaxChars)
log = log.Substring(0, MaxChars);
}
void OnGUI()
{
GUILayout.Label(log);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment