Skip to content

Commit d60bc2f

Browse files
author
wanderer
committed
添加ConsoleWindow,测试模块
1 parent 9b1d1ee commit d60bc2f

File tree

3 files changed

+252
-8
lines changed

3 files changed

+252
-8
lines changed

GameFramework/Runtime/Debugger/ConsoleWindow.cs

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
5+
using System.Reflection;
6+
47
namespace Wanderer.GameFramework
58
{
69
[DebuggerWindow("Console")]
710
public class ConsoleWindow : IDebuggerWindow
811
{
12+
//命令
13+
private List<string> _commands = new List<string>();
14+
//静态函数 反射调用
15+
private Dictionary<string, MethodInfo> _commandMethod = new Dictionary<string, MethodInfo>();
16+
//action执行函数
17+
private Dictionary<string, Action> _commandAction = new Dictionary<string, Action>();
18+
//支持反射
19+
private bool _reflectionsupported = true;
20+
//显示文本
21+
private List<string> _showTexts = new List<string>();
22+
//滚动文本的位置坐标
23+
private Vector2 _showScrollPos = Vector2.zero;
24+
//输入命令
25+
private string _inputCommand = "";
26+
927
public void OnInit(params object[] args)
1028
{
1129
}
1230

1331
public void OnEnter()
1432
{
33+
Application.logMessageReceived += OnLogMessageReceived;
1534
}
1635

1736
public void OnExit()
1837
{
38+
Application.logMessageReceived -= OnLogMessageReceived;
1939
}
2040

2141
public void OnClose()
@@ -24,7 +44,212 @@ public void OnClose()
2444

2545
public void OnDraw()
2646
{
47+
GUILayout.BeginHorizontal();
48+
if (GUILayout.Button("clear", GUILayout.Width(60)))
49+
{
50+
ClearLines();
51+
}
52+
_reflectionsupported = GUILayout.Toggle(_reflectionsupported, "Reflection supported");
53+
GUILayout.EndHorizontal();
54+
55+
_showScrollPos = GUILayout.BeginScrollView(_showScrollPos, "box");
56+
foreach (var item in _showTexts)
57+
{
58+
if (item.StartsWith("$ "))
59+
{
60+
GUILayout.Label(item);
61+
}
62+
else
63+
{
64+
GUILayout.BeginVertical("box");
65+
GUILayout.Label(item);
66+
GUILayout.EndVertical();
67+
}
68+
}
69+
GUILayout.EndScrollView();
70+
71+
GUILayout.BeginVertical("box", GUILayout.Height(60));
72+
GUILayout.BeginHorizontal("box");
73+
_inputCommand = GUILayout.TextField(_inputCommand, GUILayout.Width(500));
74+
GUILayout.Space(10);
75+
if (GUILayout.Button("Exec", GUILayout.Width(60), GUILayout.Height(25)))
76+
{
77+
ExecuteCommand(_inputCommand.Trim());
78+
_inputCommand = "";
79+
}
80+
GUILayout.EndHorizontal();
81+
GUILayout.EndVertical();
82+
}
83+
84+
#region 外部接口
85+
/// <summary>
86+
/// 添加命令
87+
/// </summary>
88+
/// <param name="command"></param>
89+
/// <param name="callAction"></param>
90+
public void AddCommand(string command, Action callAction)
91+
{
92+
if (!_commandAction.ContainsKey(command))
93+
{
94+
_commandAction.Add(command, callAction);
95+
}
96+
}
97+
/// <summary>
98+
/// 添加命令
99+
/// </summary>
100+
/// <param name="command"></param>
101+
/// <param name="callMethod"></param>
102+
public void AddCommand(string command, MethodInfo callMethod)
103+
{
104+
if (!_commandMethod.ContainsKey(command))
105+
{
106+
_commandMethod.Add(command, callMethod);
107+
}
108+
}
109+
/// <summary>
110+
/// 移除命令
111+
/// </summary>
112+
/// <param name="command"></param>
113+
public void RemoveCommand(string command)
114+
{
115+
if (_commandAction.ContainsKey(command))
116+
{
117+
_commandAction.Remove(command);
118+
}
119+
if (_commandMethod.ContainsKey(command))
120+
{
121+
_commandMethod.Remove(command);
122+
}
123+
}
124+
#endregion
125+
126+
127+
#region 事件回调
128+
//事件回调
129+
private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
130+
{
131+
string log = $"<color={GetLogTypeColor(type)}>[{type.ToString()}]</color> {condition}";
132+
AddLine(log);
133+
}
134+
#endregion
135+
136+
#region 内部函数
137+
138+
private void AddLine(string line)
139+
{
140+
_showTexts.Add(line);
141+
_showScrollPos.y = float.MaxValue;
142+
}
143+
144+
//执行命令
145+
private void ExecuteCommand(string command)
146+
{
147+
if (string.IsNullOrEmpty(command))
148+
return;
149+
150+
//0 返回
151+
int defaultResult = ExecuteDefaultCommand(command);
152+
switch (defaultResult)
153+
{
154+
case 0:
155+
return;
156+
}
157+
158+
//普通命令
159+
AddLine($"$ <color=green>{command}</color>");
160+
if (_commandAction.TryGetValue(command, out Action callAction))
161+
{
162+
callAction.Invoke();
163+
return;
164+
}
165+
166+
//静态函数命令
167+
string[] args = command.Split(' ');
168+
string[] parameters = null;
169+
if (args != null && args.Length > 1)
170+
{
171+
command = args[0];
172+
//动态添加参数
173+
parameters = new string[args.Length - 1];
174+
Array.Copy(args, 1, parameters, 0, parameters.Length);
175+
}
176+
MethodInfo callMethod;
177+
if (!_commandMethod.TryGetValue(command, out callMethod))
178+
{
179+
if (_reflectionsupported)
180+
{
181+
int index = command.LastIndexOf('.');
182+
if (index > 0 && index < command.Length - 1)
183+
{
184+
string typefullName = command.Substring(0, index);
185+
index++;
186+
string methodName = command.Substring(index, command.Length - index);
187+
Type callType = TypeUtility.AllAssemblyTypes.Find(x => x.FullName.Equals(typefullName));
188+
if (callType != null)
189+
{
190+
callMethod = callType.GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
191+
}
192+
}
193+
}
194+
}
195+
196+
if (callMethod != null)
197+
{
198+
bool call = false;
199+
var gp = callMethod.GetParameters();
200+
if (gp.Length == 0)
201+
{
202+
parameters = null;
203+
call = true;
204+
}
205+
else if (gp.Length == parameters.Length)
206+
{
207+
call = true;
208+
}
209+
if (call)
210+
{
211+
callMethod.Invoke(null, parameters);
212+
return;
213+
}
214+
}
215+
//添加反馈
216+
AddLine($"<color=yellow>$ [{command}]</color> Can't find command or parameters error!");
217+
}
218+
219+
//执行默认的命令
220+
private int ExecuteDefaultCommand(string command)
221+
{
222+
switch (command)
223+
{
224+
case "clear":
225+
ClearLines();
226+
return 0;
227+
}
228+
return -1;
229+
}
230+
231+
//清理数据
232+
private void ClearLines()
233+
{
234+
_showTexts.Clear();
235+
}
236+
237+
private string GetLogTypeColor(LogType type)
238+
{
239+
switch (type)
240+
{
241+
case LogType.Assert:
242+
case LogType.Error:
243+
case LogType.Exception:
244+
return "red";
245+
case LogType.Warning:
246+
return "yellow";
247+
case LogType.Log:
248+
return "white";
249+
}
250+
return "white";
27251
}
252+
#endregion
28253

29254
}
30255
}

GameFramework/Runtime/Debugger/DebuggerManager.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public DebuggerManager()
8181
}
8282
}
8383

84-
instance.OnInit();
84+
instance.OnInit(_consoleSkin);
8585
//日志窗口特殊处理
8686
if (instance is LogWindow)
8787
{
@@ -101,7 +101,7 @@ public void OnImGui()
101101
GUISkin lastGuiSkin = GUI.skin;
102102
Matrix4x4 lastMatrix = GUI.matrix;
103103

104-
GUI.skin = _consoleSkin;
104+
// GUI.skin = _consoleSkin;
105105
GUI.matrix = Matrix4x4.Scale(new Vector3(WindowScale, WindowScale, 1f));
106106
if (_showFullWindow)
107107
{
@@ -127,6 +127,25 @@ public override void OnClose()
127127
}
128128
}
129129

130+
/// <summary>
131+
/// 获取窗口
132+
/// </summary>
133+
/// <typeparam name="T"></typeparam>
134+
public T GetWindow<T>() where T : class, IDebuggerWindow
135+
{
136+
if (_allDebuggerWindows != null)
137+
{
138+
for (int i = 0; i < _allDebuggerWindows.Count; i++)
139+
{
140+
if (_allDebuggerWindows[i].GetType() == typeof(T))
141+
{
142+
return (T)_allDebuggerWindows[i];
143+
}
144+
}
145+
}
146+
return null;
147+
}
148+
130149
#region 内部函数
131150
//绘制大窗口
132151
private void DrawDebuggerFullWindow(int windowId)

GameFramework/Runtime/Resources/Console/ConsoleSkin.guiskin

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ MonoBehaviour:
1616
m_box:
1717
m_Name: box
1818
m_Normal:
19-
m_Background: {fileID: 2800000, guid: 2af4b7279e8baae4e92e1d9afa082a99, type: 3}
19+
m_Background: {fileID: 0}
2020
m_ScaledBackgrounds: []
2121
m_TextColor: {r: 0.79999995, g: 0.79999995, b: 0.79999995, a: 1}
2222
m_Hover:
@@ -150,11 +150,11 @@ MonoBehaviour:
150150
m_toggle:
151151
m_Name: toggle
152152
m_Normal:
153-
m_Background: {fileID: 2800000, guid: 152c1055f879a514aa7d41cbe5edd2ee, type: 3}
153+
m_Background: {fileID: 0}
154154
m_ScaledBackgrounds: []
155155
m_TextColor: {r: 0.89112896, g: 0.89112896, b: 0.89112896, a: 1}
156156
m_Hover:
157-
m_Background: {fileID: 2800000, guid: 85ff3d698cbc3474b8f618de96265d61, type: 3}
157+
m_Background: {fileID: 0}
158158
m_ScaledBackgrounds: []
159159
m_TextColor: {r: 1, g: 1, b: 1, a: 1}
160160
m_Active:
@@ -204,7 +204,7 @@ MonoBehaviour:
204204
m_Font: {fileID: 12800000, guid: 458c9354c373bb84b8f358a7c97ab64f, type: 3}
205205
m_FontSize: 9
206206
m_FontStyle: 0
207-
m_Alignment: 1
207+
m_Alignment: 3
208208
m_WordWrap: 0
209209
m_RichText: 1
210210
m_TextClipping: 1
@@ -418,7 +418,7 @@ MonoBehaviour:
418418
m_window:
419419
m_Name: window
420420
m_Normal:
421-
m_Background: {fileID: 2800000, guid: 1b5446581fa67e346b6f3ba659dfc250, type: 3}
421+
m_Background: {fileID: 2800000, guid: 83368ef7151c04649b4c1707658443f8, type: 3}
422422
m_ScaledBackgrounds: []
423423
m_TextColor: {r: 1, g: 1, b: 1, a: 1}
424424
m_Hover:

0 commit comments

Comments
 (0)