繁体   English   中英

SmartFoxServer出现Unity记录问题

[英]SmartFoxServer with Unity Logging issue

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

我的问题是:当我有四个客户端在运行(一个在Unity中,另外三个在运行已编译的版本)并且我运行了Debug.Log代码时,它将在每个实例还是在Unity实例上运行?

仅供参考,当我进行构建时,我只是进行常规构建。 我没有检查开发版本。 从服务器返回响应时,我看到奇怪的行为。 有时,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版本的调试)或4次(对于运行的每个游戏实例一次)。

谢谢

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方法,或者该类未附加到层次结构中的更多对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM