This post describes a quick “tip” for improving how you work with logging in Unity. Share it with your team in case you find it useful 🙂
Console Window Mess
As developers we probably spend 50% of our time debugging code (it is my blog, so i get make up fake statistics). Any Unity developer who spends more than 5 minutes with the editor probably knows the Debug class, and more specifically the Debug.Log method that prints log messages to the Console window.
While it’s easy to log messages, the Console window can quickly get cluttered and it becomes super hard to find the exact trace message you’re looking for.
Rich text formatting seems to be one of Unity’s best kept secrets (haven’t seen people using it yet), but it can come in handy for tidying up the Console window.
Rich Text Formatting
Log messages passed to Debug.Log and its other variants (LogError, LogWarning, etc) can include special “tags” that provide formatting for the log message, for example:
Debug.Log("<color=green>This is a green message!</color>");
These tags control the visual properties of the displayed text, such as its size and color. For all the fine details check out the documentation here.
The only issue is that it can be tedious to manually format all log messages, which may drive you to not use this feature at all.
Luckily, there’s a better option. Enter the “Extension Method” solution.
String Extensions Class
Instead of manually formatting every log message and adding the required tags, we can create a static extensions class, and define a few extension methods on the string type.
The following code snippet adds a few extension methods on the string class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class StringLoggingExtensions | |
{ | |
/// <summary> | |
/// Sets the color of the text according to the parameter value. | |
/// </summary> | |
/// <param name="message">Message.</param> | |
/// <param name="color">Color.</param> | |
public static string Colored(this string message, Colors color) | |
{ | |
return string.Format("<color={0}>{1}</color>", color.ToString(), message); | |
} | |
/// <summary> | |
/// Sets the color of the text according to the traditional HTML format parameter value. | |
/// </summary> | |
/// <param name="message">Message</param> | |
/// <param name="color">Color</param> | |
public static string Colored(this string message, string colorCode) | |
{ | |
return string.Format("<color={0}>{1}</color>", colorCode, message); | |
} | |
/// <summary> | |
/// Sets the size of the text according to the parameter value, given in pixels. | |
/// </summary> | |
/// <param name="message">Message.</param> | |
/// <param name="size">Size.</param> | |
public static string Sized(this string message, int size) | |
{ | |
return string.Format ("<size={0}>{1}</size>", size, message); | |
} | |
/// <summary> | |
/// Renders the text in boldface. | |
/// </summary> | |
/// <param name="message">Message.</param> | |
public static string Bold(this string message) | |
{ | |
return string.Format ("<b>{0}</b>", message); | |
} | |
/// <summary> | |
/// Renders the text in italics. | |
/// </summary> | |
/// <param name="message">Message.</param> | |
public static string Italics(this string message) | |
{ | |
return string.Format ("<i>{0}</i>", message); | |
} | |
} | |
public enum Colors | |
{ | |
aqua, | |
black, | |
blue, | |
brown, | |
cyan, | |
darkblue, | |
fuchsia, | |
green, | |
grey, | |
lightblue, | |
lime, | |
magenta, | |
maroon, | |
navy, | |
olive, | |
purple, | |
red, | |
silver, | |
teal, | |
white, | |
yellow | |
} |
This allows writing simple code, without having to manually add the formatting tags:
// Update is called once per frame void Update () { var message = "Hello, World!"; // Print the message in green. Debug.Log (message.Colored (Colors.green)); // Compose different formatting together. Debug.Log (message.Bold().Sized(16)); // Works with LogError as well. Debug.LogError (message.Italics()); }
The code snippet above prints out the following logs in the Console window, with the proper formatting:
Notes
- Rich text formatting works with all Debug.LogXXX methods.
- Editor GUI controls also support rich formatting, however it must be explicitly enabled.
GUIStyle style = new GUIStyle (); style.richText = true;