Created
June 26, 2022 13:25
-
-
Save sbtoonz/c5ad9ceed4602cc26f2b6f583a7e5b2d to your computer and use it in GitHub Desktop.
Show Console output in OnGui method for Unity
This file contains 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 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