扩展Debug.Log()以便随时关掉输出Log信息
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;
}
}