Unity 彩色打印日志信息

/*
 *FileName:      MT_Debug.cs
 *Author:        MinTao
 *Date:          2021/12/21 11:56:18
 *UnityVersion:  2020.3.0f1c1
 *Description:
*/

using System.Collections.Generic;

public static class MT_Debug
{
    /// <summary>
    /// 可否打印
    /// </summary>
    public static bool Logable = true;

    /// <summary>
    /// 打印颜色
    /// </summary>
    public enum COLOR
    {
        /// <summary>
        /// 白色
        /// </summary>
        WHITE,

        /// <summary>
        /// 红色
        /// </summary>
        RED,

        /// <summary>
        /// 黄色
        /// </summary>
        YELLOW,

        /// <summary>
        /// 绿色
        /// </summary>
        GREEN,

        /// <summary>
        /// 橘色
        /// </summary>
        ORANGE,

        /// <summary>
        /// 灰色
        /// </summary>
        GRAY,

        /// <summary>
        /// 浅蓝色
        /// </summary>
        BABYBLUE,
    }

    static readonly Dictionary<COLOR, string> ColorType = new Dictionary<COLOR, string>
    {
        {COLOR.WHITE, "#FFFFFF"},
        {COLOR.RED, "#FF0000"},
        {COLOR.YELLOW, "#FFFF00"},
        {COLOR.GREEN, "#00FF00"},
        {COLOR.ORANGE, "#FFA500"},
        {COLOR.GRAY, "#999999"},
        {COLOR.BABYBLUE, "#00EEEE"},
    };


    private static string SetColor(this string str, COLOR color)
    {
        str = string.Format("<color={0}>{1}</color>", ColorType[color], str);
        return str;
    }

    /// <summary>
    /// 打印
    /// </summary>
    /// <param name="str">打印信息</param>
    public static void Log(params object[] str)
    {
        if (!Logable)
        {
            return;
        }

        string log = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (i == str.Length - 1 && i > 0)
            {
                log += str[i];
            }
            else
            {
                log += str[i] + "      ";
            }
        }

        if (log == string.Empty)
        {
            log = ">>>>>>>>>>>>>>>>>>>>>>>>";
        }

        UnityEngine.Debug.Log(log);
    }

    /// <summary>
    /// 打印
    /// </summary>
    /// <param name="color">打印颜色</param>
    /// <param name="str">打印信息</param>
    public static void Log(COLOR color, params object[] str)
    {
        if (!Logable)
        {
            return;
        }

        string log = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (i == str.Length - 1 && i > 0)
            {
                log += str[i];
            }
            else
            {
                log += str[i] + "      ";
            }
        }

        if (log == string.Empty)
        {
            log = ">>>>>>>>>>>>>>>>>>>>>>>>";
        }

        UnityEngine.Debug.Log(log.SetColor(color));
    }

    /// <summary>
    /// 换行打印
    /// </summary>
    /// <param name="str">打印信息</param>
    public static void LogLine(params object[] str)
    {
        if (!Logable)
        {
            return;
        }

        string log = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (i == str.Length - 1 && i > 0)
            {
                log += str[i];
            }
            else
            {
                log += str[i] + "\n";
            }
        }

        if (log == string.Empty)
        {
            log = ">>>>>>>>>>>>>>>>>>>>>>>>";
        }

        UnityEngine.Debug.Log(log);
    }

    /// <summary>
    /// 换行打印
    /// </summary>
    /// <param name="color">打印颜色</param>
    /// <param name="str">打印信息</param>
    public static void LogLine(COLOR color, params object[] str)
    {
        if (!Logable)
        {
            return;
        }

        string log = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (i == str.Length - 1 && i > 0)
            {
                log += str[i];
            }
            else
            {
                log += str[i] + "\n";
            }
        }

        if (log == string.Empty)
        {
            log = ">>>>>>>>>>>>>>>>>>>>>>>>";
        }

        UnityEngine.Debug.Log(log.SetColor(color));
    }

    /// <summary>
    /// 打印错误
    /// </summary>
    /// <param name="str">打印信息</param>
    public static void LogError(params object[] str)
    {
        if (!Logable)
        {
            return;
        }

        string log = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (i == str.Length - 1 && i > 0)
            {
                log += str[i];
            }
            else
            {
                log += str[i] + "      ";
            }
        }

        if (log == string.Empty)
        {
            log = ">>>>>>>>>>>>>>>>>>>>>>>>";
        }

        UnityEngine.Debug.Log(log.SetColor(COLOR.RED));
    }

    /// <summary>
    /// 打印警告
    /// </summary>
    /// <param name="str">打印信息</param>
    public static void LogWarning(params object[] str)
    {
        if (!Logable)
        {
            return;
        }

        string log = string.Empty;
        for (int i = 0; i < str.Length; i++)
        {
            if (i == str.Length - 1 && i > 0)
            {
                log += str[i];
            }
            else
            {
                log += str[i] + "      ";
            }
        }

        if (log == string.Empty)
        {
            log = ">>>>>>>>>>>>>>>>>>>>>>>>";
        }

        UnityEngine.Debug.Log(log.SetColor(COLOR.YELLOW));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值