1
- using System . Collections ;
1
+ using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using UnityEngine ;
5
+ using System . Reflection ;
6
+
4
7
namespace Wanderer . GameFramework
5
8
{
6
9
[ DebuggerWindow ( "Console" ) ]
7
10
public class ConsoleWindow : IDebuggerWindow
8
11
{
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
+
9
27
public void OnInit ( params object [ ] args )
10
28
{
11
29
}
12
30
13
31
public void OnEnter ( )
14
32
{
33
+ Application . logMessageReceived += OnLogMessageReceived ;
15
34
}
16
35
17
36
public void OnExit ( )
18
37
{
38
+ Application . logMessageReceived -= OnLogMessageReceived ;
19
39
}
20
40
21
41
public void OnClose ( )
@@ -24,7 +44,212 @@ public void OnClose()
24
44
25
45
public void OnDraw ( )
26
46
{
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" ;
27
251
}
252
+ #endregion
28
253
29
254
}
30
255
}
0 commit comments