Skip to content

Instantly share code, notes, and snippets.

@Crushy
Created December 30, 2013 16:52
Show Gist options
  • Save Crushy/8184637 to your computer and use it in GitHub Desktop.
Save Crushy/8184637 to your computer and use it in GitHub Desktop.
In game debug console using DF GUI
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
[RequireComponent(typeof(dfRichTextLabel))]
public class InGameDebug : MonoBehaviour {
private Queue<string> myLogQueue = new Queue<string>();
private string output = "";
public string stack = "";
public int maxLines = 30;
private StringBuilder strBuild;
private dfRichTextLabel ourLabel;
public string ConsoleText {
get {
return strBuild.ToString();
}
}
void Awake() {
if ( !Debug.isDebugBuild || Application.isEditor ) {
Destroy( this.gameObject );
return;
}
ourLabel = this.GetComponent<dfRichTextLabel>();
strBuild = new StringBuilder( maxLines * 200 );
}
void OnEnable() {
Application.RegisterLogCallback( HandleLog );
}
void OnDisable() {
Application.RegisterLogCallback( null );
}
public void HandleLog( string logString, string stackTrace, LogType type ) {
output = logString;
stack = stackTrace;
string newString = "\n [" + type + "] : " + output;
string colour;
switch ( type ) {
case LogType.Error:
colour = "red";
break;
case LogType.Exception:
colour = "orange";
break;
case LogType.Warning:
colour = "yellow";
break;
default:
colour = "white";
break;
}
myLogQueue.Enqueue( "<p style=\"color: " + colour + "\">" + newString + "</p>" );
if ( type == LogType.Exception) {
newString = "\n" + "<pre style=\" margin: 25px; size: 16\">" + stackTrace + "</pre>";
myLogQueue.Enqueue( newString );
}
while ( myLogQueue.Count > maxLines ) {
myLogQueue.Dequeue();
}
strBuild.Length = 0;
foreach ( string s in myLogQueue ) {
strBuild.Append(s);
}
ourLabel.Text = this.ConsoleText;
ourLabel.ScrollToBottom();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment