Skip to content

Commit e240b61

Browse files
author
wanderer
committed
控制台支持向GameObject发送消息
1 parent e8e769e commit e240b61

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

GameFramework/Runtime/Debugger/ConsoleWindow.cs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public class ConsoleWindow : IDebuggerWindow
1616
//action执行函数
1717
private Dictionary<string, Action> _commandAction = new Dictionary<string, Action>();
1818
//支持反射
19-
private bool _reflectionsupported = true;
19+
private bool _reflectionSupported = true;
20+
//支持消息发送
21+
private bool _sendMessageSupported = true;
2022
//显示文本
2123
private List<string> _showTexts = new List<string>();
2224
//滚动文本的位置坐标
@@ -49,7 +51,8 @@ public void OnDraw()
4951
{
5052
ClearLines();
5153
}
52-
_reflectionsupported = GUILayout.Toggle(_reflectionsupported, "Reflection supported");
54+
_reflectionSupported = GUILayout.Toggle(_reflectionSupported, "Reflection supported");
55+
_sendMessageSupported = GUILayout.Toggle(_sendMessageSupported, "SendMessage supported");
5356
GUILayout.EndHorizontal();
5457

5558
_showScrollPos = GUILayout.BeginScrollView(_showScrollPos, "box");
@@ -176,42 +179,57 @@ private void ExecuteCommand(string command)
176179
MethodInfo callMethod;
177180
if (!_commandMethod.TryGetValue(command, out callMethod))
178181
{
179-
if (_reflectionsupported)
182+
183+
int index = command.LastIndexOf('.');
184+
if (index > 0 && index < command.Length - 1)
180185
{
181-
int index = command.LastIndexOf('.');
182-
if (index > 0 && index < command.Length - 1)
186+
string fullName = command.Substring(0, index);
187+
index++;
188+
string methodName = command.Substring(index, command.Length - index);
189+
//优先查找反射
190+
if (_reflectionSupported)
183191
{
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));
192+
Type callType = TypeUtility.AllAssemblyTypes.Find(x => x.FullName.Equals(fullName));
188193
if (callType != null)
189194
{
190195
callMethod = callType.GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
196+
197+
if (callMethod != null)
198+
{
199+
bool call = false;
200+
var gp = callMethod.GetParameters();
201+
if (gp.Length == 0)
202+
{
203+
parameters = null;
204+
call = true;
205+
}
206+
else if (gp.Length == parameters.Length)
207+
{
208+
call = true;
209+
}
210+
if (call)
211+
{
212+
callMethod.Invoke(null, parameters);
213+
return;
214+
}
215+
}
191216
}
192217
}
193-
}
194-
}
195218

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;
219+
//支持查找GameObject SendMessage
220+
if (_sendMessageSupported)
221+
{
222+
GameObject findGameObject = GameObject.Find(fullName);
223+
if (findGameObject != null)
224+
{
225+
findGameObject.SendMessage(methodName, args);
226+
return;
227+
}
228+
}
213229
}
214230
}
231+
232+
215233
//添加反馈
216234
AddLine($"<color=yellow>$ [{command}]</color> Can't find command or parameters error!");
217235
}

0 commit comments

Comments
 (0)