Skip to content

Instantly share code, notes, and snippets.

@sbtoonz
Created June 26, 2022 13:25
Show Gist options
  • Save sbtoonz/c5ad9ceed4602cc26f2b6f583a7e5b2d to your computer and use it in GitHub Desktop.
Save sbtoonz/c5ad9ceed4602cc26f2b6f583a7e5b2d to your computer and use it in GitHub Desktop.
Show Console output in OnGui method for Unity
using UnityEngine;
namespace Closure
{
public class ConsoleToGui : MonoBehaviour
{
//#if !UNITY_EDITOR
static string myLog = "";
private string output;
private string stack;
void OnEnable()
{
Application.logMessageReceived += Log;
}
void OnDisable()
{
Application.logMessageReceived -= Log;
}
public void Log(string logString, string stackTrace, LogType type)
{
output = logString;
stack = stackTrace;
myLog = output + "\n" + myLog;
if (myLog.Length > 5000)
{
myLog = myLog.Substring(0, 4000);
}
}
void OnGUI()
{
//if (!Application.isEditor) //Do not display in editor ( or you can use the UNITY_EDITOR macro to also disable the rest)
{
myLog = GUI.TextArea(new Rect(10, 10, 250, 500), myLog);
}
}
//#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment