Let me debug in your GUI


Here's a script I made to be able to see debug stuff while testing a game outside of the editor i.e. webplayer. It adds a button on the top right corner to turn on/off the debug window.

Should be pretty easy to use, just put it on some gameobject, and alter the "maxlines" value to fit your need.

using UnityEngine;
using System.Collections;

public class MyLog : MonoBehaviour
{
static string myLog;
static Queue myLogQueue = new Queue();
public string output = "";
public string stack = "";
private bool hidden = true;
private Vector2 scrollPos;
public int maxLines = 30;

void OnEnable()
{
Application.RegisterLogCallback(HandleLog);
}

void OnDisable()
{
Application.RegisterLogCallback(null);
}

void HandleLog(string logString, string stackTrace, LogType type)
{
output = logString;
stack = stackTrace;
string newString = "\n [" + type + "] : " + output;
myLogQueue.Enqueue(newString);
if (type == LogType.Exception)
{
newString = "\n" + stackTrace;
myLogQueue.Enqueue(newString);
}

while (myLogQueue.Count > maxLines)
{
myLogQueue.Dequeue();
}

myLog = string.Empty;
foreach (string s in myLogQueue)
{
myLog += s;
}
}

void OnGUI()
{
if (!hidden)
{
GUI.TextArea(new Rect(0, 0, Screen.width / 3, Screen.height), myLog);
if (GUI.Button(new Rect(Screen.width - 100, 10, 80, 20), "Hide"))
{
hide(true);
}
}
else
{
if (GUI.Button(new Rect(Screen.width - 100, 10, 80, 20), "Show"))
{
hide(false);
}
}
}

public void hide(bool shouldHide)
{
hidden = shouldHide;
}
}

Hope you like it. If you make any improvements, please share.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 komentar:

Posting Komentar