0
\$\begingroup\$

I'm working on a project that has a lot of logging messages that pass very fast.

I can use error logs or warning logs but is there any way to log something with different colors so it is easier to find?

Right now, I just use something like this:

Debug.LogError("show missing gold popup accepted");
\$\endgroup\$
2
\$\begingroup\$

Your very first step to answer this question should be to check the Unity documentation, where you'd find a helpful code example that does exactly this:

Debug.Log("<color=red>Error: </color>AssetBundle not found");

Or if you were looking at the Debug.LogError documentation instead, you'd see:

When the message is a string, rich text markup can be used to add emphasis. See the manual page about rich text for details of the different markup tags available.

And following that link, you'd find the exact syntax for setting a colour:

color

Sets the color of the text according to the parameter value. The color can be specified in the traditional HTML format. #rrggbbaa …where the letters correspond to pairs of hexadecimal digits denoting the red, green, blue and alpha (transparency) values for the color. For example, cyan at full opacity would be specified by color=#00ffffff…

You can specify hexadecimal values in uppercase or lowercase; #FF0000 is equivalent to #ff0000.

We are <color=#ff0000ff>colorfully</color> amused

Another option is to use the name of the color. This is easier to understand but naturally, the range of colors is limited and full opacity is always assumed.

<color=cyan>some text</color>

The available color names are given in the table below.

So please do get in the habit of reading the documentation first.

This is not the first time I've observed a post of yours that could be answered or improved with a quick check of the documentation or time spent testing in-game. Please take your time and use the full resources available to you, and you'll tend to reach the correct solutions faster than by rushing to post here.

\$\endgroup\$
  • \$\begingroup\$ I've used this approach before but this isn't easy, so I made a custom language to debugging colorful text. \$\endgroup\$ – Seyed Morteza Kamali Oct 18 at 13:46
3
\$\begingroup\$

I made a class to debugging easy in Unity:

Example

using UnityEngine;

public class Example : MonoBehaviour
{
    private void Start()
    {
        Debug.Log("Hello Green" % Colorize.Green);
        Debug.Log("Hello Bold Green" % Colorize.Green % FontFormat.Bold);

        Debug.Log("Hello Orange" % Colorize.Orange);
        Debug.Log("Hello Italic Orange" % Colorize.Orange % FontFormat.Italic);

        Debug.Log(
            "Failure " % Colorize.Orange % FontFormat.Bold +
            "doesn't mean the " +            
            "game is over " % Colorize.Red % FontFormat.Bold +
            "It means try again with " +
            "experience!" % Colorize.Green % FontFormat.Bold
        );
    }
}

enter image description here


Font Color

using System;
using UnityEngine;

public class Colorize{

    // Color Example

    public static Colorize Red = new Colorize(Color.red);
    public static Colorize Yellow = new Colorize(Color.yellow);
    public static Colorize Green = new Colorize(Color.green);
    public static Colorize Blue = new Colorize(Color.blue);
    public static Colorize Cyan = new Colorize(Color.cyan);
    public static Colorize Magenta = new Colorize(Color.magenta);

    // Hex Example

    public static Colorize Orange = new Colorize("#FFA500");
    public static Colorize Olive  = new Colorize("#808000");
    public static Colorize Purple  = new Colorize("#800080");
    public static Colorize DarkRed  = new Colorize("#8B0000");
    public static Colorize DarkGreen  = new Colorize("#006400");
    public static Colorize DarkOrange  = new Colorize("#FF8C00");
    public static Colorize Gold  = new Colorize("#FFD700");

    private readonly string _prefix;

    private const string Suffix = "</color>";

    // Convert Color to HtmlString
    private Colorize(Color color){
        _prefix = $"<color=#{ColorUtility.ToHtmlStringRGB(color)}>";
    }
    // Use Hex Color
    private Colorize(string hexColor)
    {
        _prefix = $"<color={hexColor}>";
    }

    public static string operator %(string text, Colorize color){
        return color._prefix + text + Suffix;
    }


}

Font Format

using System;
using UnityEngine;

public class FontFormat{
    private string _prefix;

    private string _suffix;

    public static FontFormat Bold = new FontFormat("b");
    public static FontFormat Italic = new FontFormat("i");
    private FontFormat(string format)
    {
        _prefix = $"<{format}>";
        _suffix = $"</{format}>";
    }

    public static string operator %(string text, FontFormat textFormat)
    {
        return textFormat._prefix + text + textFormat._suffix;
    }
}

Update

I made a language to debugging easy in unity.

you can use my tool Kamali Debug.

enter image description here

    private void Start()
    {
    DebugX.Log(@"I:orange:b; love:red:b; Unity:yellow:b;
    ColorfullllllllllllllllText:rainbow:b;");

    DebugX.Log("sniper:orange:b; bug:green:b;");

    }

Variables

image

using UnityEngine;
using KamaliDebug;
public class Example : MonoBehaviour
{
    public int score;
    public int health;
    private void Start()
    {
        DebugX.Log($"Score = {score}:yellow:b;");
        DebugX.Log($"Health = {health}:green:b;");
    }
}

Syntax

(Text or Emoji):Color:TextStyle;

Font Color

DebugX.Log("Hello world:green;");

image

Font Style

  • b = Bold
  • i = Italic

    DebugX.Log("Hello:b; World:i;");

image

Emoji

DebugX.Log("love:red:b; love:red:b; love:red:b;");

image

Complex

DebugX.Log("Hello:yellow:b; world:red:b;");

image

\$\endgroup\$
  • \$\begingroup\$ great answer. thanks for edditing \$\endgroup\$ – virtouso Oct 18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.