Created
October 18, 2022 13:51
-
-
Save dyguests/1a6dfa74e30b2bf60fa77001fb734829 to your computer and use it in GitHub Desktop.
LogGui, Unity log on screen, Screen Log.
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.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