Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity script to display in-game debug console.
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// A console to display Unity's debug logs in-game.
/// </summary>
public class Console : MonoBehaviour
{
struct Log
{
public string message;
public string stackTrace;
public LogType type;
}
/// <summary>
/// The hotkey to show and hide the console window.
/// </summary>
public KeyCode toggleKey = KeyCode.BackQuote;
List<Log> logs = new List<Log>();
Vector2 scrollPosition;
bool show;
bool collapse;
// Visual elements:
static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>()
{
{ LogType.Assert, Color.white },
{ LogType.Error, Color.red },
{ LogType.Exception, Color.red },
{ LogType.Log, Color.white },
{ LogType.Warning, Color.yellow },
};
const int margin = 20;
Rect windowRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));
Rect titleBarRect = new Rect(0, 0, 10000, 20);
GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
void OnEnable ()
{
Application.RegisterLogCallback(HandleLog);
}
void OnDisable ()
{
Application.RegisterLogCallback(null);
}
void Update ()
{
if (Input.GetKeyDown(toggleKey)) {
show = !show;
}
}
void OnGUI ()
{
if (!show) {
return;
}
windowRect = GUILayout.Window(123456, windowRect, ConsoleWindow, "Console");
}
/// <summary>
/// A window that displayss the recorded logs.
/// </summary>
/// <param name="windowID">Window ID.</param>
void ConsoleWindow (int windowID)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
// Iterate through the recorded logs.
for (int i = 0; i < logs.Count; i++) {
var log = logs[i];
// Combine identical messages if collapse option is chosen.
if (collapse) {
var messageSameAsPrevious = i > 0 && log.message == logs[i - 1].message;
if (messageSameAsPrevious) {
continue;
}
}
GUI.contentColor = logTypeColors[log.type];
GUILayout.Label(log.message);
}
GUILayout.EndScrollView();
GUI.contentColor = Color.white;
GUILayout.BeginHorizontal();
if (GUILayout.Button(clearLabel)) {
logs.Clear();
}
collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
// Allow the window to be dragged by its title bar.
GUI.DragWindow(titleBarRect);
}
/// <summary>
/// Records a log from the log callback.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="stackTrace">Trace of where the message came from.</param>
/// <param name="type">Type of message (error, exception, warning, assert).</param>
void HandleLog (string message, string stackTrace, LogType type)
{
logs.Add(new Log() {
message = message,
stackTrace = stackTrace,
type = type,
});
}
}
@webflashing

This comment has been minimized.

Copy link

webflashing commented Sep 16, 2012

I was just windering, how do you send a message to the console? I tried with Console.ConsoleMessage("Test") but it's inaccesible due to it's protection level. Could you point me in the right direction here?
Thanks very much !

@DannyBen

This comment has been minimized.

Copy link

DannyBen commented Sep 23, 2012

It looks like you only need to use the standard Unity Debug.Log(), Debug.LogWarning() and Debug.LogError() and it will simply appear in this console. I haven't tried it yet, but it looks that this is the intended design.

@SrBilyon

This comment has been minimized.

Copy link

SrBilyon commented Apr 11, 2013

This is awesome. I'm using Console.Print(), but I believe any debug message can print to the console.

@MultiversalNomad

This comment has been minimized.

Copy link

MultiversalNomad commented Oct 25, 2013

This is a nifty piece of code, thanks. Made a fork showing how to use this in another script for anyone who's curious.

@mminer

This comment has been minimized.

Copy link
Owner Author

mminer commented Jan 7, 2014

@DannyBen is correct, the script uses a callback to capture messages logged using Unity's Debug class. You shouldn't need to modify your existing code to take advantage of this.

(Also, sorry for the slow reply, GitHub doesn't notify me when a Gist is commented on.)

@boxatom

This comment has been minimized.

Copy link

boxatom commented Jan 28, 2014

I love this script. thank you!

@mminer

This comment has been minimized.

Copy link
Owner Author

mminer commented Mar 28, 2014

I created a full GitHub repository for the in-game console here: https://github.com/mminer/consolation. This will allow issues to be reported and contributors to create pull requests. Changes to the Console.cs script will be pushed here also though, so if you bookmarked this page it won't disappear.

@nikhiljolly

This comment has been minimized.

Copy link

nikhiljolly commented Oct 6, 2014

This is nice. Thank you! :D

@ghost

This comment has been minimized.

Copy link

ghost commented Oct 14, 2015

Works very well ^^

@cartzhang

This comment has been minimized.

Copy link

cartzhang commented Nov 13, 2015

nice ,many thanks!

@AppDeveloperMax

This comment has been minimized.

Copy link

AppDeveloperMax commented Mar 11, 2016

How enable a debug console in empires and allies?

Regards 👍

@samusam22

This comment has been minimized.

Copy link

samusam22 commented Jun 20, 2016

Hi, i put the code in a file and after attach in empty gameObject. But when i launch exe game i dont see the console. Where is my error?

@samusam22

This comment has been minimized.

Copy link

samusam22 commented Jun 20, 2016

ok, i set black toggle key. But i have a problem. My project don't run in paly mode with unity editor, run stutters. So i try code, but i have an error that i dont see that file is. Who do i do?

@samusam22

This comment has been minimized.

Copy link

samusam22 commented Jun 20, 2016

i try with exe

@samusam22

This comment has been minimized.

Copy link

samusam22 commented Jun 20, 2016

ok, i find the option to set with i create exe file. Sorry for disturb

@goral123

This comment has been minimized.

Copy link

goral123 commented Jul 27, 2018

thx, nice piece of code

@gino8080

This comment has been minimized.

Copy link

gino8080 commented Sep 14, 2018

Great script thank you!

how to make it persist on every scene?

@Starspangledkiwi

This comment has been minimized.

Copy link

Starspangledkiwi commented Nov 1, 2019

Question, how to you change the font size? The text when the console comes up is a little too small for what I am running.

@mminer

This comment has been minimized.

Copy link
Owner Author

mminer commented Nov 4, 2019

@Starspangledkiwi: Take a look at the actively-maintained mminer/consolation repo. It has an updated Console.cs file which includes a font size option, among others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.