Unity Debug.Log 扩展

Unity Debug.Log 扩展

扩展Debug.Log()以便随时关掉输出Log信息

  • VS创 建 DLL文件
  • 编写Debug类
  • 编译并导入Unity
  • 使用

1. VS创 建 DLL

Unity Debug.Log 扩展_第1张图片
Unity Debug.Log 扩展_第2张图片

2.编写新的Debug类

(1) 导入UnityEngine.dll

  1. UnityEngine.dll路径 “unity安装路径\Editor\Data\Managed”
  2. VS:项目 -> 添加引用->浏览 选择上面路径下的UnityEngine.dll
  3. 代码如下:
using System;
using System.Collections.Generic;
using UnityEngine;

public static class EGDebug
{
    public static bool enable = true;

    public enum Color
    {
        green,
        black,
        blue,
        cyan,
        white,
        yellow,
        red
    }

    private static string Message(object message,Color color = Color.black,bool bold = false,bool italic = false)
    {
        var content = string.Format("{1}",color.ToString(), message);
        content = bold ? string.Format("{0}", content) : content;
        content = italic ? string.Format("{0}", content) : content;

        return content;
    }

    private static string Title(object title, int level, Color color)
    {
        return Message(string.Format("{0}{1}{2}{1}", Space(level * 2), Character(16 - level * 2), title), color);
    }

    private static string ObjectMessage(T content)
    {
        var result = "";

        var fields = typeof(T).GetFields();
        for (int i = 0; i < fields.Length; i++)
        {
            result += Space(4) + Message(fields[i].Name + " : ", Color.green, true) + fields[i].GetValue(content) + "\n";
        }

        var properties = typeof(T).GetProperties();
        for (int i = 0; i < properties.Length; i++)
        {
            result += Space(4) + Message(properties[i].Name + " : ", Color.green, true) + properties[i].GetValue(content, null) + "\n";
        }

        return result;
    }

    /// 
    /// 普通输出
    /// 
    /// 
    /// 
    public static void Log(object message, Color color = Color.black)
    {
        if (!enable)
        {
            return;
        }
        Debug.Log(Message(message, color));
    }

    /// 
    /// 加粗输出
    /// 
    /// 
    /// 
    public static void LogBold(object message, Color color = Color.black)
    {
        if (!enable)
        {
            return;
        }
        Debug.Log(Message(message, color, true));
    }

    /// 
    /// 斜体输出
    /// 
    /// 
    /// 
    public static void LogItalic(object message, Color color = Color.black)
    {
        if (!enable)
        {
            return;
        }
        Debug.Log(Message(message, color, false,true));
    }

    /// 
    /// 加粗斜体输出
    /// 
    /// 
    /// 
    public static void LogBoldItalic(object message, Color color = Color.black)
    {
        if (!enable)
        {
            return;
        }
        Debug.Log(Message(message, color, true, true));
    }

    /// 
    /// 输出标题栏(形如:======Title======)
    /// 
    /// 
    /// 
    /// 
    public static void LogTitle(object title, int level, Color color)
    {
        if (!enable)
        {
            return;
        }
        LogBold(Title(title, level, color));
    }

    /// 
    /// 输出对象(自定义的)
    /// 
    /// 
    /// 
    /// 
    public static void LogObject(T content, Color color = Color.black)
    {
        if (!enable)
        {
            return;
        }
        Debug.Log(Message(typeof(T).Name,color,true) + "\n" + ObjectMessage(content));
    }

    /// 
    /// 输出List
    /// 
    /// 
    /// 
    public static void LogList(List content)
    {
        if(!enable || content == null || content.Count <= 0)
        {
            return;
        }

        var result = Title(typeof(T).Name + " List", 0,Color.green) + "\n\n";
        for (int i = 0; i < content.Count; i++)
        {
            result += Title(typeof(T).Name+"_" + i, 1, Color.green) + "\n";
            result += ObjectMessage(content[i]) + "\n";
        }
        result += Title("End", 0, Color.green);

        Debug.Log(result);
    }

    /// 
    /// 输出数组
    /// 
    /// 
    /// 
    public static void LogArray(T[] content)
    {
        if (!enable || content == null || content.Length <= 0)
        {
            return;
        }

        var result = Title(typeof(T).Name + " List", 0, Color.green) + "\n";
        for (int i = 0; i < content.Length; i++)
        {
            result += Title(typeof(T).Name + "_" + i, 1, Color.green) + "\n";
            result += ObjectMessage(content[i]) + "\n";
        }
        result += Title("End", 0, Color.green);

        Debug.Log(result);
    }

    private static string Space(int number)
    {
        var space = "";
        for (int i = 0; i < number; i++)
        {
            space += " ";
        }
        return space;
    }

    private static string Character(int number, string character = "=")
    {
        var result = "";
        for (int i = 0; i < number; i++)
        {
            result += "=";
        }
        return result;
    }
}

3. 打包并导入Unity

  1. VS F7 编译
  2. 找到编译成功后的DLL文件
  3. 将其拖入Unity(Assets/Plugins)

4. 使用

EGDebug.enable = false 关掉输出信息
Unity Debug.Log 扩展_第3张图片

DLL下载链接

你可能感兴趣的:(c#)