How do I print messages and debug information to the GUI?

Since I’m trying to debug some obscure and unanswered questions about networking and variables, I’m in need of a client-server debug feature. Being that you obviously can’t debug what’s running on your server, if you start debugging on the client (unless you have two or more pcs running the project I guess, which I can’t accomplish), I would like to know if there’s a feature that’d allow you to
redirect print output, or debug output.

As of now, you can only see print and debug on the console, and I’d like to know if you can redirect their output/ pipe their output to a different device (as you’d do in perl, or dos, for example).

Of course the ideal would be to print to GUI, but if that’s not possible, even printing to a file would help greatly.

There are many solutions to this, one is even brought to you automatically.

The Unity way

The print and debug output is stored in a file called output_log.txt inside the data-folder of your runtime. To work with whats in the log callback in your application use Application.RegisterLogCallback.

Try it out with something like this:

static var myLog : String;
private var output : String = "";
private var stack : String = "";

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

function OnDisable () {
    // Remove callback when object goes out of scope
    Application.RegisterLogCallback(null);
}

function HandleLog (logString : String, stackTrace : String, type : LogType) {
    output = logString;
    stack = stackTrace;
    myLog +="
"+output;
}

function OnGUI () {
    myLog = GUI.TextArea (Rect (10, 10, Screen.width-10, Screen.height-10), myLog);
}

Your way

You could also write your own function to take care of messages:

class p {
    static var pDocument : String;
    static function log (string : String) {
        pDocument+="
"+string;
    }
}
function OnGUI () {
    myLog = GUI.TextArea (Rect (10, 10, Screen.width-10, Screen.height-10), p.pDocument);
}

Then call it with:

p.log("Hello world");

Then you could use System.IO.File.WriteAllText(filePath, pDocument) to write your own log to disc at any given time if you want to.

Debugging from afar

You also have the possibility to send your data to a server which could take care of sessions - for instance a PHP-server which stores a txt-file with the string sent from a client at any given point. This would be suitable for any of the given solutions above.

Have a look at WWWForm.

Just attach this to any gameObject:
(based on “The Unity Way” section of the answer from save)

using UnityEngine;

namespace DebugStuff
{
    public class ConsoleToGUI : MonoBehaviour
    {
//#if !UNITY_EDITOR
        static string myLog = "";
        private string output;
        private string stack;

        void OnEnable()
        {
            Application.logMessageReceived += Log;
        }

        void OnDisable()
        {
            Application.logMessageReceived -= Log;
        }

        public void Log(string logString, string stackTrace, LogType type)
        {
            output = logString;
            stack = stackTrace;
            myLog = output + "

" + myLog;
if (myLog.Length > 5000)
{
myLog = myLog.Substring(0, 4000);
}
}

        void OnGUI()
        {
            //if (!Application.isEditor) //Do not display in editor ( or you can use the UNITY_EDITOR macro to also disable the rest)
            {
                myLog = GUI.TextArea(new Rect(10, 10, Screen.width - 10, Screen.height - 10), myLog);
            }
        }
//#endif
    }
}

Usually by default, a breakpoint is set on program start, and you can then either navigate your code using the buttons at the top of the window, or if you have no code, you can customize your view to let you step through a disassembly of the binary you are looking at.chase bank login

hi, @Pressler487 @bboysil This printed sometext on my screen.
I wanted to write this code for this :
Say i have to kill 5 villans in my scene and i killed 2, so it must display sometext “2 killed,3 pending”, or something and update the progress.
Can you please help?,Hi, this wirks and sends default values.
I want to use this to print a message on game scene according to happening.
Ex : if i have killed 2 villans and 3 are pending,
It must display 2/5 killed or something
How to modify?
Please help, thank you