Skip to content

Commit 9b1d1ee

Browse files
author
wanderer
committed
完善日志窗口
1 parent 991b49e commit 9b1d1ee

File tree

6 files changed

+383
-157
lines changed

6 files changed

+383
-157
lines changed
Lines changed: 2 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
using System;
2-
using System.Collections;
1+
using System.Collections;
32
using System.Collections.Generic;
43
using UnityEngine;
5-
64
namespace Wanderer.GameFramework
75
{
86
[DebuggerWindow("Console")]
97
public class ConsoleWindow : IDebuggerWindow
108
{
11-
//当前的日志
12-
private Queue<LogNode> _logNodes = new Queue<LogNode>();
13-
private int _logMaxLine = 100;
14-
private Vector2 _logScrollPosition = Vector2.zero;
15-
169
public void OnInit(params object[] args)
1710
{
18-
_logMaxLine = (int)GameFrameworkMode.GetModule<ConfigManager>()["LogMaxLine"];
19-
Application.logMessageReceived += OnLogMessageReceived;
2011
}
2112

2213
public void OnEnter()
@@ -27,150 +18,14 @@ public void OnExit()
2718
{
2819
}
2920

30-
public void OnDraw()
31-
{
32-
GUILayout.BeginVertical("box");
33-
GUILayout.EndVertical();
34-
35-
GUILayout.BeginVertical("box");
36-
_logScrollPosition = GUILayout.BeginScrollView(_logScrollPosition, GUILayout.Height(100f));
37-
38-
foreach (LogNode logNode in _logNodes)
39-
{
40-
// switch (logNode.LogType)
41-
// {
42-
// case LogType.Log:
43-
// if (!m_InfoFilter)
44-
// {
45-
// continue;
46-
// }
47-
// break;
48-
49-
// case LogType.Warning:
50-
// if (!m_WarningFilter)
51-
// {
52-
// continue;
53-
// }
54-
// break;
55-
56-
// case LogType.Error:
57-
// if (!m_ErrorFilter)
58-
// {
59-
// continue;
60-
// }
61-
// break;
62-
63-
// case LogType.Exception:
64-
// if (!m_FatalFilter)
65-
// {
66-
// continue;
67-
// }
68-
// break;
69-
// }
70-
if (GUILayout.Toggle(null == logNode, logNode.ToString()))
71-
{
72-
// if (m_SelectedNode != logNode)
73-
// {
74-
// m_SelectedNode = logNode;
75-
// m_StackScrollPosition = Vector2.zero;
76-
// }
77-
}
78-
}
79-
GUILayout.EndScrollView();
80-
GUILayout.EndVertical();
81-
}
82-
8321
public void OnClose()
8422
{
85-
Application.logMessageReceived -= OnLogMessageReceived;
86-
}
87-
88-
89-
#region 事件回调
90-
//log 信息回调
91-
private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
92-
{
93-
_logNodes.Enqueue(LogNodePool.Get(condition, stackTrace, type));
94-
if (_logNodes.Count > _logMaxLine)
95-
{
96-
LogNodePool.Release(_logNodes.Dequeue());
97-
}
98-
}
99-
#endregion
100-
101-
}
102-
103-
internal class LogNode
104-
{
105-
public DateTime LogTime { get; private set; }
106-
public int LogFrameCount { get; private set; }
107-
public LogType LogType { get; private set; }
108-
public string LogMessage { get; private set; }
109-
public string StackTrack { get; private set; }
110-
111-
public LogNode Set(string condition, string stackTrace, LogType type)
112-
{
113-
if (type == LogType.Assert)
114-
{
115-
type = LogType.Error;
116-
}
117-
LogTime = DateTime.Now;
118-
LogFrameCount = Time.frameCount;
119-
LogType = type;
120-
LogMessage = condition;
121-
StackTrack = stackTrace;
122-
return this;
123-
}
124-
125-
public override string ToString()
126-
{
127-
Color32 color = GetStringColor();
128-
return string.Format("<color=#{0}{1}{2}{3}>[{4}][{5}] {6}</color>",
129-
color.r.ToString("x2"), color.g.ToString("x2"), color.b.ToString("x2"), color.a.ToString("x2"),
130-
LogTime.ToString("HH:mm:ss.fff"), LogFrameCount.ToString(), LogMessage);
131-
}
132-
133-
public Color GetStringColor()
134-
{
135-
Color32 color = Color.white;
136-
switch (LogType)
137-
{
138-
case LogType.Log:
139-
color = Color.white;
140-
break;
141-
142-
case LogType.Warning:
143-
color = Color.yellow;
144-
break;
145-
146-
case LogType.Error:
147-
color = Color.red;
148-
break;
149-
150-
case LogType.Exception:
151-
color = Color.red;
152-
break;
153-
}
154-
155-
return color;
15623
}
15724

158-
}
159-
160-
internal static class LogNodePool
161-
{
162-
private static readonly ObjectPool<LogNode> _objectPool = new ObjectPool<LogNode>(null, null);
163-
164-
public static LogNode Get(string condition, string stackTrace, LogType type)
25+
public void OnDraw()
16526
{
166-
return _objectPool.Get().Set(condition, stackTrace, type);
16727
}
16828

169-
public static void Release(LogNode logNode)
170-
{
171-
_objectPool.Release(logNode);
172-
}
17329
}
174-
17530
}
17631

GameFramework/Runtime/Debugger/ConsoleWindow.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GameFramework/Runtime/Debugger/DebuggerManager.cs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public class DebuggerManager : GameFrameworkModule, IImGui, IUpdate
2525
private string[] _debuggerWindowTitle;
2626
//ugui的EventSystem
2727
private EventSystem _currentEventSystem;
28-
28+
//帧率计算
2929
private FPSCounter _fpsCounter;
30+
//控制台窗口
31+
private LogWindow _logWindow;
3032

3133
public DebuggerManager()
3234
{
@@ -39,6 +41,7 @@ public DebuggerManager()
3941
List<string> _windowTitles = new List<string>();
4042
_allDebuggerWindows = new List<IDebuggerWindow>();
4143
//在当前类型
44+
List<DebuggerWindowAttribute> listDebuggerAttribute = new List<DebuggerWindowAttribute>();
4245
foreach (var item in TypeUtility.AllAssemblyTypes)
4346
{
4447
if (item.IsAbstract)
@@ -49,13 +52,45 @@ public DebuggerManager()
4952
DebuggerWindowAttribute attr = objs[0] as DebuggerWindowAttribute;
5053
if (attr != null)
5154
{
52-
_windowTitles.Add(attr.Title);
5355
IDebuggerWindow instance = (IDebuggerWindow)System.Activator.CreateInstance(item);
54-
_allDebuggerWindows.Add(instance);
56+
if (attr.Priority == 0)
57+
{
58+
listDebuggerAttribute.Add(attr);
59+
_windowTitles.Add(attr.Title);
60+
_allDebuggerWindows.Add(instance);
61+
}
62+
else
63+
{
64+
bool insert = false;
65+
for (int i = 0; i < listDebuggerAttribute.Count; i++)
66+
{
67+
if (attr.Priority < listDebuggerAttribute[i].Priority)
68+
{
69+
listDebuggerAttribute.Insert(i, attr);
70+
_windowTitles.Insert(i, attr.Title);
71+
_allDebuggerWindows.Insert(i, instance);
72+
insert = true;
73+
break;
74+
}
75+
}
76+
if (!insert)
77+
{
78+
listDebuggerAttribute.Add(attr);
79+
_windowTitles.Add(attr.Title);
80+
_allDebuggerWindows.Add(instance);
81+
}
82+
}
83+
5584
instance.OnInit();
85+
//日志窗口特殊处理
86+
if (instance is LogWindow)
87+
{
88+
_logWindow = instance as LogWindow;
89+
}
5690
}
5791
}
5892
}
93+
listDebuggerAttribute.Clear();
5994
//添加默认的Close窗口
6095
_windowTitles.Add("<b>Close</b>");
6196
_debuggerWindowTitle = _windowTitles.ToArray();
@@ -66,7 +101,7 @@ public void OnImGui()
66101
GUISkin lastGuiSkin = GUI.skin;
67102
Matrix4x4 lastMatrix = GUI.matrix;
68103

69-
// GUI.skin = _consoleSkin;
104+
GUI.skin = _consoleSkin;
70105
GUI.matrix = Matrix4x4.Scale(new Vector3(WindowScale, WindowScale, 1f));
71106
if (_showFullWindow)
72107
{
@@ -118,18 +153,30 @@ private void DrawDebuggerFullWindow(int windowId)
118153
_currentDebuggerWindow.OnEnter();
119154
}
120155
}
156+
121157
//调用窗口
122-
_currentDebuggerWindow?.OnDraw();
158+
if (_currentDebuggerWindow != null)
159+
{
160+
GUILayout.BeginVertical("window");
161+
_currentDebuggerWindow?.OnDraw();
162+
GUILayout.EndVertical();
163+
}
123164
}
124165
//绘制小窗口
125166
private void DrawDebuggerSmallWindow(int windowId)
126167
{
127168
GUI.DragWindow(_dragRect);
169+
Color defaultColor = GUI.contentColor;
170+
if (_logWindow != null)
171+
{
172+
GUI.contentColor = _logWindow.GetLogColor();
173+
}
128174
if (GUILayout.Button(_fpsCounter.FPS.ToString("f2"), GUILayout.Width(100f), GUILayout.Height(40f)))
129175
{
130176
_showFullWindow = true;
131177
SetUGuiEventSystem(false);
132178
}
179+
GUI.contentColor = defaultColor;
133180
}
134181
//设置ugui EventSystem是否激活
135182
private void SetUGuiEventSystem(bool active)

GameFramework/Runtime/Debugger/DebuggerWindowAttribute.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
namespace Wanderer.GameFramework
77
{
88
[System.AttributeUsage(AttributeTargets.Class)]
9-
public class DebuggerWindowAttribute :Attribute
9+
public class DebuggerWindowAttribute : Attribute
1010
{
11-
public string Title{get;private set;}
11+
public string Title { get; private set; }
1212

13-
public DebuggerWindowAttribute(string title)
13+
public int Priority { get; private set; }
14+
15+
public DebuggerWindowAttribute(string title, int priority = 0)
1416
{
15-
Title=title;
17+
Title = title;
18+
Priority = priority;
1619
}
1720
}
1821

0 commit comments

Comments
 (0)