in c#, tutorial, Unity3D

Console debugging in Unity made easy

Share Button

One of the most interesting feature of Unity is the ability to extend its editor and standard components. This has allowed developers all around the world to create amazing extensions which dramatically improve Unity’s usability. And, in some cases, compensate for its shortages. A very intriguing feature which is not-so-well known is the ability to customise the messages in the debug console. Rather than give you a self proclaimed ultimate solution to console debugging, this post will cover different topics to help you creating your own.

Rich Text support for Debug.Log

The standard logging function provided by Unity is Debug.Log. As reported in its documentation, it supports rich text tags. So, for instance:

a1

Adding the <color=red>  tag does the trick. Unity supports <i>  for italic, <b>  for bold and <size>  to change the pixel size. We will see later how to improve Debug  even further.

For a full list of the supported colours, you can check this page.

A better debug function

Something which bothers me deeply is the way the Debug.Log  function has to be fed. There are two main problems here: (1) Debug.Log  takes a single string and (2) its console output is quite chunky and doesn’t really allow for multi-lines. Let’s see what we can do about these things.

The code above allows to use the newly debug function by passing a list of objects without getting worried about spacing and casts:

Retrieving caller informations

The most useful feature of the Debug.Log function is that it explicitly tells the file and line of code which has generated it. .NET allows to retrieve the caller information by using the following syntax:

Unfortunately, I did not manage to get Callers annotations to work within Unity. An alternative, yet slower approach is to use the Diagnostic class which allows to look inside the current stack trace:

Editor GUI

The rich text formatting is available on the majority of Unity elements. Including its GUI. Le’t say, for instance, that we want to extend the inspector of an already existing component. This will be expanded in a further tutorial, but let’s just say for now that all you have to do is to put this script in a folder called Editor :

While DrawDefaultInspector draws the inspector for the current object, GUILayout.Label adds a text to it. By creating a GUIStyle object with the richText parameters set to true, we can draw some formatted text:

a2

Other resources

  • DebugConsole: a scrolling, interactive debug console that you can use directly in your games;
  • mminer Console: another script for in-game console;
  • WWDebugWindow: a simple, yet powerful script to write to the console;
  • Pimp my Debug.Log: an interesting set of method extensions for the string class, perfect for Debug.Log.
📧 Stay updated

A new tutorial is released every week.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

Paypal
Ko-fi
Twitter_logo

Write a Comment

Comment

  1. Thanks for mentioning my blog (Pimp my debug log).

    Regarding what you wrote: “Unfortunately, I did not manage to get Callers annotations to work within Unity”

    This is mostly a compiler feature AFAIK (apart from the attributes). You can probably achieve this yourself, but you’ll have to compile your code outside of Unity (e.g: through Visual Studio).

    Read more here: http://www.thomaslevesque.com/2012/06/13/using-c-5-caller-info-attributes-when-targeting-earlier-versions-of-the-net-framework/

  2. One thing I found useful if you want to add the calling method name to your debug statement is to use the System.Reflection library and use MethodBase.GetCurrentMethod() to get the calling method name.

    Not sure on the performance difference here though vs the Diagnostic class.