Thursday, March 29, 2012

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.

3 comments:

  1. Thanks for the awesome script - I converted it to UNITYSCRIPT (JS). Works perfectly.

    static var myLog : String ;
    static var myLogQueue = new Queue();
    public var output : String = "";
    public var stack : String = "";
    private var hidden : boolean = true;
    private var scrollPos : Vector2;
    public var maxLines : int = 30;

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

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

    function HandleLog(logString: String, stackTrace : String, type : LogType )
    {
    output = logString;
    stack = stackTrace;
    var newString : String = "\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;
    for (var s in myLogQueue)
    {
    myLog += s;
    }
    }



    function OnGUI()
    {
    if (!hidden)
    {

    myLog= GUI.TextArea(Rect(0, 0, Screen.width / 3, Screen.height), myLog);

    if (GUI.Button(Rect(Screen.width - 100, 10, 80, 20), "Hide"))
    {
    hide(true);
    }
    }
    else
    {
    if (GUI.Button(Rect(Screen.width - 100, 10, 80, 20), "Show"))
    {
    hide(false);
    }
    }
    }

    function hide(shouldHide : boolean )
    {
    hidden = shouldHide;
    }

    ReplyDelete