具有 Unity 日志记录问题的 SmartFoxServer

SmartFoxServer with Unity Logging issue

我对 Debug.Log 如何在 Unity 中使用 SmartFoxServer 工作有疑问。我正在写一个多人游戏。游戏将始终有四名玩家。由于 Unity/Visual Studio 的问题,我无法使用 Visual Studio 调试器(每次遇到断点时它都会使 Unity 崩溃)。所以,我使用 Debug.Log。

我的问题是:当我有四个客户端 运行ning(一个在 Unity 中,另外三个来自 运行ning 编译版本)并且我有一个 Debug.Log 代码 运行,是针对每个实例还是仅针对 Unity 实例 运行?

仅供参考,当我进行构建时,我只是进行正常构建。我没有检查 Development Build。当我从服务器收到响应时,我看到了奇怪的行为。有时,Debug.Log 会打印 4 次,有时只打印一次。我可以调试我的 Java 扩展,断点只命中一次。

下面是一些示例 Unity C# 代码:

public void OnExtensionResponse(BaseEvent evt) {        
        string cmd = (string)evt.Params["cmd"];
        SFSObject dataObject = (SFSObject)evt.Params["params"];
        Debug.Log("Got response from server: " + cmd + " " + dataObject.GetUtfString("gameStatus"));
        switch ( cmd ) {


        }

有时上面的 Debug.Log 代码会被调用一次,有时会调用 2 次或 5 次。根据日志记录的工作方式,我预计 1 次(它只针对 Unity 版本 运行ning 进行调试)或四次(每个游戏实例一次 运行ning)。

谢谢

对于每个实例,Debug.Log 都会 运行,如果您想查看编译版本(我假设是 exe)上的消息,那么我建议您构建一个名为 class 的 Debug_UI 它的唯一目的是将来自 Debug.Log 的所有消息显示到 OnGui 方法中。首先使用您要记录的消息调用静态函数,该函数将调用 Debug.Log 并将该日志插入静态列表中,该列表将用于在 OnGui 上显示这些消息。

// 带有 DebugMessage 函数的静态实用程序 Class

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 Class

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 ) {


        }

对于多次调用的消息,您应该检查您没有在其他 class 中实现 OnExtensionResponse 方法,或者这个 class 没有附加到更多对象在层次结构中。