Skip to content

Instantly share code, notes, and snippets.

@MultiversalNomad
Forked from mminer/Console.cs
Last active November 4, 2020 16:19
Show Gist options
  • Save MultiversalNomad/7161544 to your computer and use it in GitHub Desktop.
Save MultiversalNomad/7161544 to your computer and use it in GitHub Desktop.
This fork splits Console.cs into ConsoleInGame.cs and ExampleSceneScript.cs. See code comments for details.
/*
This fork splits Console.cs into ConsoleInGame.cs and ExampleSceneScript.cs.
ConsoleInGame is a static class containing the main console code while ExampleSceneScript.cs
is an example for showing how to use the console in other scripts.
*/
using UnityEngine;
using System;
using System.Collections.Generic;
/// <summary>
/// A console that displays the contents of Unity's debug log.
/// </summary>
/// <remarks>
/// Developed by Matthew Miner (www.matthewminer.com)
/// Forked by Joseph Cassano (jplc.ca) from Console to ConsoleInGame so the console can work from other scripts.
/// Fork adds ExampleSceneScript.cs to show how to use this class in another script.
/// Permission is given to use this script however you please with absolutely no restrictions.
/// </remarks>
public static class ConsoleInGame
{
public static readonly Version version = new Version(1, 0);
struct ConsoleMessage
{
public readonly string message;
public readonly string stackTrace;
public readonly LogType type;
public ConsoleMessage (string message, string stackTrace, LogType type)
{
this.message = message;
this.stackTrace = stackTrace;
this.type = type;
}
}
static List<ConsoleMessage> entries = new List<ConsoleMessage>();
static Vector2 scrollPos;
static bool collapse;
// Visual elements:
static GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
static GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
/// <summary>
/// A window displaying the logged messages.
/// </summary>
/// <param name="windowID">The window's ID.</param>
public static void ConsoleWindow (int windowID)
{
scrollPos = GUILayout.BeginScrollView(scrollPos);
// Go through each logged entry
for (int i = 0; i < entries.Count; i++) {
ConsoleMessage entry = entries[i];
// If this message is the same as the last one and the collapse feature is chosen, skip it
if (collapse && i > 0 && entry.message == entries[i - 1].message) {
continue;
}
// Change the text colour according to the log type
switch (entry.type) {
case LogType.Error:
case LogType.Exception:
GUI.contentColor = Color.red;
break;
case LogType.Warning:
GUI.contentColor = Color.yellow;
break;
default:
GUI.contentColor = Color.white;
break;
}
GUILayout.Label(entry.message);
}
GUI.contentColor = Color.white;
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
// Clear button
if (GUILayout.Button(clearLabel)) {
entries.Clear();
}
// Collapse toggle
collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
// Set the window to be draggable by the top title bar
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
/// <summary>
/// Logged messages are sent through this callback function.
/// </summary>
/// <param name="message">The message itself.</param>
/// <param name="stackTrace">A trace of where the message came from.</param>
/// <param name="type">The type of message: error/exception, warning, or assert.</param>
public static void HandleLog (string message, string stackTrace, LogType type)
{
ConsoleMessage entry = new ConsoleMessage(message, stackTrace, type);
entries.Add(entry);
}
}
using UnityEngine;
/// <summary>
/// An example of a script for a Unity scene that uses ConsoleInGame to make a debug log console.
/// </summary>
/// <remarks>
/// Forked by Joseph Cassano (jplc.ca) from Matthew Miner (www.matthewminer.com)'s Console.cs.
/// Fork splits Console.cs into ConsoleInGame.cs for the main console code and ExampleSceneScript
/// to show how one would use ConsoleInGame from another script.
/// Permission is given to use this script however you please with absolutely no restrictions.
/// </remarks>
public class ExampleSceneScript : MonoBehaviour
{
// Variables for use with the console.
KeyCode consoleToggleKey;
int consoleMargin;
Rect consoleWindowRect;
bool consoleShow;
void OnEnable()
{
// Tying the Log messages to the console.
Application.RegisterLogCallback(ConsoleInGame.HandleLog);
}
void OnDisable()
{
// Removing the link between the Log and the console.
Application.RegisterLogCallback(null);
}
// Use this for initialization
void Start()
{
consoleToggleKey = KeyCode.BackQuote;
consoleMargin = 20;
consoleWindowRect = new Rect(consoleMargin, consoleMargin, Screen.width - (2 * consoleMargin), Screen.height - (2 * consoleMargin));
// Example of Log message created before Update() still being shown in the console
Debug.Log("Start Log message.");
}
// Update is called once per frame
void Update()
{
// Input for displaying/dismissing the console.
if (Input.GetKeyDown(consoleToggleKey))
{
consoleShow = !consoleShow;
}
// Creating a Log message to show that the console works.
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Example Log message.");
}
// Insert the code for your scene below.
// Also add to any other methods as necessary (like Start()).
// ...
}
void OnGUI()
{
// Displaying/dismissing the console.
if (!consoleShow) {
return;
}
consoleWindowRect = GUILayout.Window(123456, consoleWindowRect, ConsoleInGame.ConsoleWindow, "Console");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment