- Home /
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.
Answer by save · Aug 03, 2011 at 07:44 AM
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 +="\n"+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+="\n"+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.
You could of course also have all clients send the server information with their log callback, which would centralize everything. Then you could even have functions on the server alerting from its clients. It all depends on how important the debug feature is and how big of a project this is.
Just a tip:
Also, when working with text on screen that is updating by itself it is easiest to use a GUI-skin that makes sure the text starts at bottom to update upwards (this way you always see the latest information without the need of a scroll function).
Or just switch the order of
pDocument+="\n"+string;
to
pDocument=string+"\n"+pDocument;
Answer by bboysil · Mar 18, 2015 at 03:57 PM
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 + "\n" + 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
}
}
This one is implemented with UGUI and looks better: https://www.assetstore.unity3d.com/#!/content/44935
Hello, I have used your method and everything is displayed the way you have described perfectly. but I'm looking to display the transform and rotation values in real-time, such as a moving car, where its coordinates are changing constantly. how can i see these values changing in the console? if the object (car in my case) is selected in the Hierarchy tab, i can see the values changing inside the inspector, when the project is played. i would like to have the same values but on the console using your GUI translation methode. to debug the position of my object i'm using this script:
========= using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CarPosition : $$anonymous$$onoBehaviour { public GameObject Car; // Use this for initialization void Start () { Debug.Log ("Car Realtime position X " + Car.transform.position.x + " Y " + Car.transform.position.y + " Z " + Car.transform.position.z); Debug.Log ("Car Realtime rotation X " + Car.transform.rotation.x + " Y " + Car.transform.rotation.y + " Z " + Car.transform.rotation.z); print (transform.position); print (transform.localRotation); } // Update is called once per frame void Update () { } } =====
Your help is highly appreciated,
$$anonymous$$any Thanks,
Issam
Answer by Pressler487 · Jul 26, 2018 at 10:31 AM
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
Answer by fathimaneha3108 · Aug 19, 2020 at 11:32 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612165151im_/https://answers.unity.com/themes/thub/images/avi.jpg)