As your game is slowly progressing, you will often find that the number of logs you are doing to the Unity Console keeps increasing. Of course, using debug logging is extremely helpful to figuring out what is exactly happing in your game. However, too much debug logging can also make it more obfuscated and confusing. One way to deal with this is to add extra formatting to the Unity console through the use of formatting Rich Text. Let’s have a look at how we can format Rich Text in the Unity Console.
The Idea
In this article we will examine how we can make logs in the console more distinguishable from the other We will do this using special Rich Text formatting commands and try applying them in an intelligent way. In the end you should be able to do something like this:

How it Works
The developers behind Unity did think of ways to format Rich Text in the Unity Console. Actually, they had to because the default warning and error messages are shown using Rich Text. Ever noticed warnings are displayed using yellow/orange text and errors using red? That’s achieved using Rich Text formatting.
In order to apply Rich Text in the Unity Console, we need to add some extra tags to the string that we try to output. If you have ever worked with html in the past then this will look quite familiar. The following tags are available for us to manipulate:
- <color> To change the color of the text
- <b> To make the text bold
- <i> To make the text italic
- <size> To change the size of the text
For example, the following code will render the result below. As can be seen,, the tags can be combined to create more elaborate formatting options.
Debug.Log("<color=#ff9900><b>Hello</b> <i>World!</i></color>");

Why Creating a Class is Useful
Even though it looks straight forward to simply add tags to your debugging output, there are a several advantages why you would want to put this type of functionality into some kind of a class. Simply adding tags to your raw debug output has the following disadvantages:
- The actual string that you are trying to log becomes obfuscated with tags. This makes it harder for a developer to understand the code.
- Writing out the tags manually is prone to human error. You might forget to close a tag or make a typo with catastrophic results.
- There is no way to actually disable the tags in a build, resulting in log files to be obfuscated with tags as well. Sending a developer a log file of your game that is filled with formatting tags is going to confuse them and make it harder for them to read through it.
The solution is to create a class that actually manages the formatting for us. For our purpose, we will be making use of Method Extensions. If you are unfamiliar with these, we suggest you to read up on Method Extensions.
Creating the Class
To start, we will simply create a class that creates a set of Method Extensions for the default C# String class. This will allow us to add chaining operations to a string value. In the code below we will also be adding our class to the Debugging.Styling namespace. This is to make sure the Method Extensions will only become available to us when explicitly including the Debugging.Styling namespace in our code
namespace Debugging.Styling { public static class DebugStringExtensions { public static string Color(this string self, Color color) { return string.Format("<color=#{0}>{1}</color>", ColorUtility.ToHtmlStringRGB(color), self); } public static string Bold(this string self) { return string.Format("<b>{0}</b>", self); } public static string Italic(this string self) { return string.Format("<i>{0}</i>", self); } public static string Size(this string self, int size) { return string.Format("<size={0}>{1}</size>", size, self); } } }
Remember the code we used to log the Hello World! string to the console? With our new Method Extensions in place, we are able to do the following. You might argue that the code still feels obfuscated because of all the different method calls. However, it is already much easier to read and understand what is going on.
Debug.Log("Hello".Bold().Color(Color.yellow) +" World!".Italic().Color(Color.green));

Disabling it
Now that we are able to format our debug logging with Rich Text, we also would like to disable it when we are testing our game outside of the Unity Editor. There are various ways of implementing this, yet in this case we will be using the UNITY_EDITOR Preprocessor Directive. This directive is only defined while your code is compiled for use in the Unity Editor. Using this directive has the benefit of not incurring any runtime costs as the check is done on compile time.
public static string Color(this string self, Color color) { #if UNITY_EDITOR return string.Format("<color=#{0}>{1}</color>", ColorUtility.ToHtmlStringRGB(color), self); #else return self; #endif } public static string Bold(this string self) { #if UNITY_EDITOR return string.Format("<b>{0}</b>", self); #else return self; #endif }
As can be seen in the code above, we simply implement the Method Extensions in two ways. First, we create one implementation when the UNITY_EDITOR directive is defined which simply applies our formatting. Next, we create another implementation when the UNITY_EDITOR directive is not defined (during a build). In the latter case, we simply do nothing and return the value of the string as it was passed to the method.
Adding Extra Functionality
Adding methods to colorize your text or to make it bold is a nice start, yet we can also add other functionality in order to format Rich Text in the Unity Console
1. Complete Styling
We can add a new Method Extension that allows us to set multiple styles in one go. The following is an example of such an extension. Feel free to add as many variants as you please.
public static string Stylize(this string self, Color color, bool bold, bool italic) { #if UNITY_EDITOR self = self.Color(color); if(bold) { self = self.Bold(); } if(italic) { self = self.Italic(); } #endif return self; }
2. Quotations and Parenthesis
Similarly to applying colors and sizes to our Rich Text, we can do the same with Quotations and Parenthesis. The code below will wrap the passed string into double quotes or parenthesis respectively. Another nice feature is that these methods don’t need any UNITY_EDITOR preprocessor directive since we don’t care if they end up in the build or not.
public static string Quote(this string self) { return string.Format("\"{0}\"", self); } public static string Parenthesis(this string self) { return string.Format("({0})", self); }
3. Blackouts
The final feature that we can implement is something we refer to as blackouts. This is simply a method that partially or completely hides a particular string value. We can use this to log data that might be more sensitive. Preferable, we should not log sensitive data but we added it here for completion. In the code below you can specify how many characters you want to be hidden (replaced with X).
public static string Blackout(this string self, int visible) { visible = visible > self.Length ? self.Length : visible; int length = self.Length-visible; if(length > 0) { return self.Replace(self.Substring(0, length), new string('x', length)); } else { return self; } }
Debug Channels
To fully utilize Rich Text formatting in the Unity Console we also recommend implementing so called Debug Channels. This way, different game system have their own dedicated channel on which they can log data with each channel having their own specified formatting. This also has the advantage that your debug calls won’t be obfuscated as much with formatting calls as seen throughout the article.
Conclusion
By this time, we hope to have given you some ideas on how you can format Rich Text in the Unity Console. and why this is preferable. Please note that there are already several plugins out there on the market that offer similar functionality out of the box. It is up to you to decide what best suits your project needs.