Unity原生的Debug.Log即使在发布版也是会输出Log的,而且知道了在编辑器中输出Log还可以设置文字颜色与大小之后,就想着封装一下来使用。
1: public class DebugLog
2: {
3: /// <summary>
4: /// 是否打开Log输出,如果为false,则所有的log将不会显示
5: /// </summary>
6: public static bool bEnableLog = true;
7: /// <summary>
8: /// 显示当前使用该属性的类的名称
9: /// </summary>
10: public static string CurrentClass
11: {
12: get
13: {
14: var st = new System.Diagnostics.StackTrace();
15: var index = Mathf.Min(st.FrameCount - 1, 2);
16: if (index < 0)
17: return "{NoClass}";
18: return "{" + st.GetFrame(index).GetMethod().DeclaringType.Name + "}";
19: }
20: }
21:
22: region Tag,可以用于忽略某特定的log
23: private static HashSet<string> _setIgnoreTag = new HashSet<string>();
24: /// <summary>
25: /// 添加要忽略的tag名称
26: /// </summary>
27: /// <param name="strTag">要忽略的tag</param>
28: public static void AddIgnoreTag(string strTag)
29: {
30: _setIgnoreTag.Add(strTag);
31: }
32: /// <summary>
33: /// 取消对某tag的忽略
34: /// </summary>
35: /// <param name="strTag">要取消忽略的tag</param>
36: public static void RemoveIgnoreTag(string strTag)
37: {
38: _setIgnoreTag.Remove(strTag);
39: }
40: /// <summary>
41: /// 清空要忽略的tag,即所有tag都不会忽略
42: /// </summary>
43: public static void ClearIgnoreTag()
44: {
45: _setIgnoreTag.Clear();
46: }
47: /// <summary>
48: /// 是否要忽略某tag的log输出
49: /// </summary>
50: /// <param name="strTag">要检测的tag</param>
51: /// <returns></returns>
52: private static bool IsIgnoreTag(string strTag)
53: {
54: return _setIgnoreTag.Contains(strTag);
55: }
56: endregion
57:
58:
59: region _log_
60: /// <summary>
61: /// Log输出,类似Debug.Log
62: /// </summary>
63: /// <param name="strLog">log内容</param>
64: /// <param name="context"></param>
65: public static void Log(string strLog, UnityEngine.Object context = null)
66: {
67: if (!bEnableLog)
68: {
69: return;
70: }
71: UnityEngine.Debug.Log(strLog, context);
72: }
73: /// <summary>
74: /// Log输出加强版, 可以设置tag, 如果该tag在忽略列表里面则该log不会输出
75: /// </summary>
76: /// <param name="strLog">log内容</param>
77: /// <param name="strTag">tag</param>
78: /// <param name="strColor">内容显示的颜色,格式为 RRGGBB</param>
79: /// <param name="context"></param>
80: public static void Log(string strLog, string strTag, string strColor = "ffffff", UnityEngine.Object context = null)
81: {
82: if (!bEnableLog
83: || IsIgnoreTag(strTag))
84: {
85: return;
86: }
87:
88: UnityEngine.Debug.Log(string.Format("[{0}]:<color=#{1}>{2}</color>", strTag, strColor, strLog), context);
89: }
90: /// <summary>
91: /// Debug.LogWarning
92: /// </summary>
93: /// <param name="strLog"></param>
94: /// <param name="context"></param>
95: public static void LogWarning(string strLog, UnityEngine.Object context = null)
96: {
97: if (!bEnableLog)
98: {
99: return;
100: }
101: UnityEngine.Debug.LogWarning(strLog, context);
102: }
103: /// <summary>
104: /// LogWarning加强版
105: /// </summary>
106: /// <param name="strLog">log内容</param>
107: /// <param name="strTag">tag</param>
108: /// <param name="strColor">内容显示的颜色,格式为 RRGGBB</param>
109: /// <param name="context"></param>
110: public static void LogWarning(string strLog, string strTag, string strColor = "ffff00", UnityEngine.Object context = null)
111: {
112: if (!bEnableLog
113: || IsIgnoreTag(strTag))
114: {
115: return;
116: }
117:
118: UnityEngine.Debug.LogWarning(string.Format("[{0}]:<color=#{1}>{2}</color>", strTag, strColor, strLog), context);
119: }
120: /// <summary>
121: /// Debug.LogError
122: /// </summary>
123: /// <param name="strLog"></param>
124: /// <param name="context"></param>
125: public static void LogError(string strLog, UnityEngine.Object context = null)
126: {
127: if (!bEnableLog)
128: {
129: return;
130: }
131: UnityEngine.Debug.LogError(strLog, context);
132: }
133: /// <summary>
134: /// LogError加强版
135: /// </summary>
136: /// <param name="strLog">log内容</param>
137: /// <param name="strTag">tag</param>
138: /// <param name="strColor">内容显示的颜色,格式为 RRGGBB</param>
139: /// <param name="context"></param>
140: public static void LogError(string strLog, string strTag, string strColor = "ff0000", UnityEngine.Object context = null)
141: {
142: if (!bEnableLog
143: || IsIgnoreTag(strTag))
144: {
145: return;
146: }
147:
148: UnityEngine.Debug.LogError(string.Format("[{0}]:<color=#{1}>{2}</color>", strTag, strColor, strLog), context);
149: }
150: endregion
151:
152: region _Tools_
153: /// <summary>
154: /// 可以为指定的字符串转为带颜色的RichText
155: /// 比如 strLog = “哈哈哈”,color = red
156: /// 则返回的字符串为 "<color=#ff0000>哈哈哈</color>"
157: /// </summary>
158: /// <param name="strLog">内容</param>
159: /// <param name="color">颜色</param>
160: /// <returns>转换后的携带color的字符串</returns>
161: public static string ColorSurround(string strLog, Color color)
162: {
163: if UNITY_EDITOR
164: return "<color=" + GetColorName(color) + ">" + strLog + "</color>";
165: else
166: return strLog;
167: endif
168: }
169: /// <summary>
170: /// 可以为指定的字符串转为带大小的RichText
171: /// 比如 strLog = "哈哈哈", size = 25
172: /// 则返回的字符串为 "<size=25>哈哈哈</size>"
173: /// </summary>
174: /// <param name="strLog">内容</param>
175: /// <param name="nSize">文字大小</param>
176: /// <returns>转换后的携带size的字符串</returns>
177: public static string SizeSurround(string strLog, int nSize = 25)
178: {
179: if UNITY_EDITOR
180: return "<size=" + nSize + ">" + strLog + "</size>";
181: else
182: return strLog;
183: endif
184: }
185: endregion
186: }
简单封装之后测试可以,但双击Console中的输出的时候会打开这个DebugLog文件,而不是我们调用这个DebugLog的地方,一个方法就是将这个类导出为DLL,然后将dll放到项目的Plugins中即可。当然最好将这个类放到自己的命名空间中去。
它会自动给你新建一个类文件,你可以删掉或修改一下。
可以将DebugLog类放到这里,然后因为里面会用到Unity的东西,所以要添加引用的依赖库:
右键—>添加–》引用…
在界面中选中UnityEngine.dll
如果没有的话点击下面的浏览,到你的Unity路径下面找到这个文件就可以。
还有一步是修改目标框架:
在项目上右键–》属性–》应用程序的目标框架选择 Unity3.5 net subset base class libraries,这个知道原因的可以自己选择,不知道原因的就选这个就可以:
然后编译生成,可以Debug或Release生成,通过选择:
生成成功就可以在项目路径的Debug或Release文件夹中找到你的.dll了,复制你的dll到Unity的Plugins目录下即可使用。
如果你会频繁修改这个文件,而每次都要复制dll到Plugins下面很麻烦的话,VS还提供生成后事件,我们可以编辑一个当生成成功之后就复制这个dll到Plugins的事件,这样在每次编译成功之后都会自动复制到Plugins下面:
项目右键—>属性—>生成事件:
这个可以有!
OH,GOOD BLOG.
Happy New Year.
看看!
年前再来转转!