本教程封装了Debug的模块,实现了以下几种功能
1.自定义打开关闭
2.自定义Tag
3.打印时间戳
4.保存打印至文件
5.Format
上代码:
Debuger:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
using System; using System.IO; using UnityEngine; using System.Reflection; using System.Diagnostics; /// <summary> /// Unity 的 Debug 的封装类 /// </summary> public class Debuger { /// <summary> /// 是否输出打印 /// </summary> public static bool EnableLog = true; /// <summary> /// 是否显示打印时间 /// </summary> public static bool EnableTime = true; /// <summary> /// 是否储存到文本 /// </summary> public static bool EnableSave = false; /// <summary> /// 是否显示堆栈打印信息 /// </summary> public static bool EnableStack = true; /// <summary> /// 打印文本保存文件夹路径 /// </summary> public static string LogFileDir = Application.persistentDataPath + "/DebugerLog/"; /// <summary> /// 打印文本名称 /// </summary> private static string LogFileName = ""; /// <summary> /// 打印前缀 /// </summary> private static string Prefix = "-> "; /// <summary> /// 打印文本流 /// </summary> private static StreamWriter LogFileWriter = null; public static void Log(object message) { message = "<color=#00ff00>" + message + "</color>"; bool flag = !Debuger.EnableLog; if (!flag) { string str = Debuger.GetLogTime() + message; UnityEngine.Debug.Log(Debuger.Prefix + str, null); Debuger.LogToFile("[I]" + str, false); } } public static void Log(object message, object context) { message = "<color=#00ff00>" + message + "</color>"; bool flag = !Debuger.EnableLog; if (!flag) { string str = Debuger.GetLogTime() + message; UnityEngine.Debug.Log(Debuger.Prefix + str, (UnityEngine.Object)context); Debuger.LogToFile("[I]" + str, false); } } public static void Log(string tag, object message) { tag = "<color=#800080ff>" + tag + "</color>"; message = "<color=#00ff00>" + message + "</color>"; bool flag = !Debuger.EnableLog; if (!flag) { message = Debuger.GetLogTime(tag, message); UnityEngine.Debug.Log(Debuger.Prefix + message, null); Debuger.LogToFile("[I]" + message, false); } } public static void Log(string tag, string format, params object[] args) { tag = "<color=#800080ff>" + tag + "</color>"; string message = "<color=#00ff00>" + string.Format(format, args) + "</color>"; bool flag = !Debuger.EnableLog; if (!flag) { string logText = Debuger.GetLogTime(tag, message); UnityEngine.Debug.Log(Debuger.Prefix + logText, null); Debuger.LogToFile("[I]" + logText, false); } } public static void LogWarning(object message) { message = "<color=#ffff00ff>" + message + "</color>"; string str = Debuger.GetLogTime() + message; UnityEngine.Debug.LogWarning(Debuger.Prefix + str, null); Debuger.LogToFile("[W]" + str, false); } public static void LogWarning(object message, object context) { message = "<color=#ffff00ff>" + message + "</color>"; string str = Debuger.GetLogTime() + message; UnityEngine.Debug.LogWarning(Debuger.Prefix + str, (UnityEngine.Object)context); Debuger.LogToFile("[W]" + str, false); } public static void LogWarning(string tag, object message) { tag = "<color=#800080ff>" + tag + "</color>"; message = "<color=#ffff00ff>" + message + "</color>"; message = Debuger.GetLogTime(tag, message); UnityEngine.Debug.LogWarning(Debuger.Prefix + message, null); Debuger.LogToFile("[W]" + message, false); } public static void LogWarning(string tag, string format, params object[] args) { tag += "<color=#800080ff>" + tag + "</color>"; string message = "<color=#ffff00ff>" + string.Format(format, args) + "</color>"; string logText = Debuger.GetLogTime(tag, string.Format(format, args)); UnityEngine.Debug.LogWarning(Debuger.Prefix + logText, null); Debuger.LogToFile("[W]" + logText, false); } public static void LogError(object message) { message = "<color=#ff0000ff>" + message + "</color>"; string str = Debuger.GetLogTime() + message; UnityEngine.Debug.LogError(Debuger.Prefix + str, null); Debuger.LogToFile("[E]" + str, true); } public static void LogError(object message, object context) { message = "<color=#ff0000ff>" + message + "</color>"; string str = Debuger.GetLogTime() + message; UnityEngine.Debug.LogError(Debuger.Prefix + str, (UnityEngine.Object)context); Debuger.LogToFile("[E]" + str, true); } public static void LogError(string tag, object message) { tag = "<color=#800080ff>" + tag + "</color>"; message = "<color=#ff0000ff>" + message + "</color>"; message = Debuger.GetLogTime(tag, message); UnityEngine.Debug.LogError(Debuger.Prefix + message, null); Debuger.LogToFile("[E]" + message, true); } public static void LogError(string tag, string format, params object[] args) { tag += "<color=#800080ff>" + tag + "</color>"; string message = "<color=#ff0000ff>" + string.Format(format, args) + "</color>"; string logText = Debuger.GetLogTime(tag, string.Format(format, args)); UnityEngine.Debug.LogError(Debuger.Prefix + logText, null); Debuger.LogToFile("[E]" + logText, true); } /// <summary> /// 获取打印时间 /// </summary> /// <param name="tag">触发打印信息对应的类或者NAME字段名称</param> /// <param name="message"></param> /// <returns></returns> private static string GetLogTime(string tag, object message) { string result = ""; bool enableTime = Debuger.EnableTime; if (enableTime) { result = DateTime.Now.ToString("HH:mm:ss.fff") + " "; } return result + tag + "::" + message; } /// <summary> /// 获取打印时间 /// </summary> /// <returns></returns> private static string GetLogTime() { string result = ""; bool enableTime = Debuger.EnableTime; if (enableTime) { result = DateTime.Now.ToString("HH:mm:ss.fff") + " "; } return result; } /// <summary> /// 序列化打印信息 /// </summary> /// <param name="message">打印信息</param> /// <param name="EnableStack">是否开启堆栈打印</param> private static void LogToFile(string message, bool EnableStack = false) { bool flag = !Debuger.EnableSave; if (!flag) { bool flag2 = Debuger.LogFileWriter == null; if (flag2) { Debuger.LogFileName = DateTime.Now.GetDateTimeFormats('s')[0].ToString(); Debuger.LogFileName = Debuger.LogFileName.Replace("-", "_"); Debuger.LogFileName = Debuger.LogFileName.Replace(":", "_"); Debuger.LogFileName = Debuger.LogFileName.Replace(" ", ""); Debuger.LogFileName += ".log"; bool flag3 = string.IsNullOrEmpty(Debuger.LogFileDir); if (flag3) { Debuger.LogFileDir = Application.persistentDataPath + "/DebugerLog/"; } string path = Debuger.LogFileDir + Debuger.LogFileName; try { bool flag4 = !Directory.Exists(Debuger.LogFileDir); if (flag4) { Directory.CreateDirectory(Debuger.LogFileDir); } Debuger.LogFileWriter = File.AppendText(path); Debuger.LogFileWriter.AutoFlush = true; } catch (Exception ex2) { Debuger.LogFileWriter = null; UnityEngine.Debug.LogError("LogToCache() " + ex2.Message + ex2.StackTrace, null); return; } } bool flag5 = Debuger.LogFileWriter != null; if (flag5) { try { Debuger.LogFileWriter.WriteLine(message); bool flag6 = (EnableStack || Debuger.EnableStack); if (flag6) { Debuger.LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace()); } } catch (Exception) { } } } } } |
DebugerExtension:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
using System; using System.Diagnostics; using System.Reflection; /// <summary> /// 自定义Debuger类的扩展类 /// </summary> public static class DebugerExtension { /// <summary> /// Debug.Log扩展,用法与Debug.Log相符 /// </summary> /// <param name="obj">触发函数对应的类</param> /// <param name="message">打印信息</param> public static void Log(this object obj, string message) { bool flag = !Debuger.EnableLog; if (!flag) { Debuger.Log(DebugerExtension.GetLogTag(obj), message); } } /// <summary> /// Debug.Log扩展,用法与Debug.Log相符 /// </summary> /// <param name="obj">触发函数对应的类</param> /// <param name="format"></param> /// <param name="args"></param> public static void Log(this object obj, string format, params object[] args) { bool flag = !Debuger.EnableLog; if (!flag) { string message = string.Format(format, args); Debuger.Log(DebugerExtension.GetLogTag(obj), message); } } /// <summary> /// Debug.LogWarning扩展,用法与Debug.LogWarning相符 /// </summary> /// <param name="obj">触发函数对应的类</param> /// <param name="message">打印信息</param> public static void LogWarning(this object obj, string message) { Debuger.LogWarning(DebugerExtension.GetLogTag(obj), message); } /// <summary> /// Debug.LogWarning扩展,用法与Debug.LogWarning相符 /// </summary> /// <param name="obj">触发函数对应的类</param> /// <param name="format"></param> /// <param name="args"></param> public static void LogWarning(this object obj, string format, params object[] args) { string message = string.Format(format, args); Debuger.LogWarning(DebugerExtension.GetLogTag(obj), message); } /// <summary> /// Debug.LogError扩展,用法与Debug.LogError相符 /// </summary> /// <param name="obj">触发函数对应的类</param> /// <param name="message">打印信息</param> public static void LogError(this object obj, string message) { Debuger.LogError(DebugerExtension.GetLogTag(obj), message); } /// <summary> /// Debug.LogError扩展,用法与Debug.LogError相符 /// </summary> /// <param name="obj">触发函数对应的类</param> /// <param name="format"></param> /// <param name="args"></param> public static void LogError(this object obj, string format, params object[] args) { string message = string.Format(format, args); Debuger.LogError(DebugerExtension.GetLogTag(obj), message); } /// <summary> /// 获取调用打印的类名称或者标记有NAME的字段 /// 有NAME字段的,触发类名称用NAME字段对应的赋值 /// 没有用类的名称代替 /// </summary> /// <param name="obj">触发Log对应的类</param> /// <returns></returns> private static string GetLogTag(object obj) { FieldInfo field = obj.GetType().GetField("NAME"); bool flag = field != null; string result; if (flag) { result = (string)field.GetValue(obj); } else { result = obj.GetType().Name; } return result; } } |
测试代码:
1 2 3 4 5 6 7 8 |
void Start () { Debuger.Log("测试代码1"); Debuger.LogWarning("测试代码2"); Debuger.LogError("测试代码3"); Debuger.Log("Test", "测试代码4"); Debuger.Log("Test", "测试代码{0}", 5); this.Log("测试代码6"); } |
测试结果:
当输出打印内容后,我们通常会双击打印信息,想定位到打印的位置。
这个需求我们只需要把两个脚本打成DLL导入到工程里,就可以啦!
这里提供DLL下载地址:链接: https://pan.baidu.com/s/18QBSp-ZdE1QRtugAN-gijw 提取码: isrd
- 本文固定链接: http://www.u3d8.com/?p=1804
- 转载请注明: 网虫虫 在 u3d8.com 发表过