Skip to content

Instantly share code, notes, and snippets.

@AkhmadMax
Forked from mminer/Console.cs
Last active January 20, 2019 09:23
Show Gist options
  • Save AkhmadMax/5675581 to your computer and use it in GitHub Desktop.
Save AkhmadMax/5675581 to your computer and use it in GitHub Desktop.
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)
/// Permission is given to use this script however you please with absolutely no restrictions.
///
/// FPS counter added in this fork
/// Align added
/// </remarks>
public class DebugConsole : MonoBehaviour
{
public static readonly Version version = new Version(1, 2);
public enum Align
{
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
public Align align = Align.BottomLeft;
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;
}
}
public KeyCode toggleKey = KeyCode.BackQuote;
List<ConsoleMessage> entries = new List<ConsoleMessage>();
Vector2 scrollPos;
bool show;
bool collapse;
// Visual elements:
const int margin = 20;
Rect windowRect = new Rect(margin, margin, 300, 400);
GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
// fps counter
public double updateInterval = 0.5F;
private double lastInterval;
private int frames = 0;
private double fps;
void OnEnable() { Application.RegisterLogCallback(HandleLog); }
void OnDisable() { Application.RegisterLogCallback(null); }
void Start()
{
lastInterval = Time.realtimeSinceStartup;
frames = 0;
if (align == Align.BottomRight || align == Align.TopRight)
windowRect.x = Screen.width - windowRect.width - margin;
if (align == Align.BottomLeft || align == Align.BottomRight)
windowRect.y = Screen.height - windowRect.height - margin;
}
void Update()
{
if (Input.GetKeyDown(toggleKey))
{
show = !show;
}
// Calculation of FPS
if (show)
{
++frames;
float timeNow = Time.realtimeSinceStartup;
if (timeNow > lastInterval + updateInterval)
{
fps = frames / (timeNow - lastInterval);
frames = 0;
lastInterval = timeNow;
}
}
}
void OnGUI()
{
if (!show)
{
return;
}
windowRect = GUILayout.Window(123456, windowRect, ConsoleWindow, "FPS: " + fps.ToString("f0"));
}
/// <summary>
/// A window displaying the logged messages.
/// </summary>
/// <param name="windowID">The window's ID.</param>
void ConsoleWindow(int windowID)
{
scrollPos = GUILayout.BeginScrollView(scrollPos);
// Go through each logged entry
for (int i = entries.Count - 1; i >= 0; 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>
void HandleLog(string message, string stackTrace, LogType type)
{
ConsoleMessage entry = new ConsoleMessage(message, stackTrace, type);
entries.Add(entry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment