Debug.Log 将为每个实例运行,如果您想查看已编译版本上的消息(我假设为 exe),那么我建议您构建一个名为 Debug_UI 的类,它的唯一目的是将 Debug.Log 中的所有消息显示到OnGui 方法。首先使用您要记录的消息调用静态函数,该函数将调用 Debug.Log 并将该日志插入到静态列表中,该列表将用于在 OnGui 上显示这些消息。
// 带有 DebugMessage 函数的静态实用程序类
public static List<string> logs= new List<string>();
public static void DebugMessage (string logType, string message) {
logs.Add(message);
if (logType.Equals("warning"))
Debug.LogWarning(message);
else if (logType.Equals("regular"))
Debug.Log(message);
else if (logType.Equals("error"))
Debug.LogError(message);
}
// Debug_UI 类
private bool _display;
private bool _log;
public Vector2 scrollPosition;
void OnGUI()
{
if (GUILayout.Button("Log")) _log = !_log;
if (_log)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height-130));
for(int i= Utilities.logs.Count-1; i >0; i--)
{
GUILayout.Label(Utilities.logs[i]);
}
GUILayout.EndScrollView();
if (GUILayout.Button("Clear"))
Utilities.logs.Clear();
if (GUILayout.Button("Copy To Clipboard"))
GUIUtility.systemCopyBuffer = CopyToClipboard();
}
}
private string CopyToClipboard()
{
string response = null;
for (int i = Utilities.logs.Count - 1; i > 0; i--)
{
response += Utilities.logs[i] + "\n";
}
return response;
}
// 你将如何在你的代码中使用它
public void OnExtensionResponse(BaseEvent evt) {
string cmd = (string)evt.Params["cmd"];
SFSObject dataObject = (SFSObject)evt.Params["params"];
Utilities.Text.DebugMessage("normal","Got response from server: " + cmd + " " + dataObject.GetUtfString("gameStatus"));
switch ( cmd ) {
}
至于多次调用的消息,您应该检查您没有在其他类中实现 OnExtensionResponse 方法,或者该类没有附加到层次结构中的更多对象。