Unity3D打印拓展XMDebug

unity3d自带的Debug用起来是在是恶心,打印多个变量需要用“+”来连接,还要用空格来区分隔开,简直恶心到不要不要的。

我自己写个Debug的拓展,方便代码打印。

1.自定义打印颜色;

2.可变参数;

3.全局控制打印输出。

// **********************************************************************
// Copyright (C) XM
// Author: 吴肖牧
// Date: 2018-05-15
// Desc: 
// **********************************************************************

using System.Collections.Generic;

public static class XMDebug
{
    /// <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));
    }
}

为了快点实现脚本,代码有点冗余,强迫症的可以自己修改。

最后我们可以加个代码片段,快速的实现打印,简直爽到不要不要的。

d+e+tab自动打出XMDebug.Log();

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>de</Title>
			<Shortcut>de</Shortcut>
			<Description>XMDebug.Log 的代码片段</Description>
			<Author>XM 吴肖牧</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal Editable="false">
					<ID>XMLogXMDebug</ID>
					<Function>SimpleTypeName(global::Log)</Function>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[XMDebug.Log($end$);]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>


猜你喜欢

转载自blog.csdn.net/yye4520/article/details/80393954