简体   繁体   English

SmartFoxServer出现Unity记录问题

[英]SmartFoxServer with Unity Logging issue

I have a question about how Debug.Log works in Unity, with SmartFoxServer. 我有一个关于Debug.Log如何在Unity中与SmartFoxServer一起工作的问题。 I am writing a multiplayer game. 我正在写一个多人游戏。 The game will always have four players. 游戏将始终有四个玩家。 Due to issues with Unity/Visual Studio, I cannot use the Visual Studio Debugger (It crashes Unity every time I hit a break point). 由于Unity / Visual Studio的问题,我无法使用Visual Studio调试器(每次遇到断点时,它都会使Unity崩溃)。 So, I use Debug.Log. 因此,我使用Debug.Log。

My question is this: When I have four clients running (one is in Unity, the other three are from running the compiled build) and I have a Debug.Log code run, will it run for every instance or just the Unity instance? 我的问题是:当我有四个客户端在运行(一个在Unity中,另外三个在运行已编译的版本)并且我运行了Debug.Log代码时,它将在每个实例还是在Unity实例上运行?

FYI, when I do a build, I just do a normal build. 仅供参考,当我进行构建时,我只是进行常规构建。 I don't have Development Build checked. 我没有检查开发版本。 I am seeing odd behavior when I get a response back from the server. 从服务器返回响应时,我看到奇怪的行为。 Sometimes, the Debug.Log will print 4 times and sometimes just once. 有时,Debug.Log将打印4次,有时仅打印一次。 I can debug my Java extension and the break point is only hit once. 我可以调试我的Java扩展,并且断点仅命中一次。

Here is some example Unity C# code: 这是一些示例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 ) {


        }

Sometimes the Debug.Log code, above, gets called once, sometimes 2 times, or 5 times. 有时上面的Debug.Log代码被调用一次,有时被调用2次或5次。 Depending on how Logging works, I would expect 1 time (it it only debugs for the Unity version running) or four times (once for each instance of the game running). 根据日志记录的工作方式,我希望使用1次(仅针对运行Unity版本的调试)或4次(对于运行的每个游戏实例一次)。

thanks 谢谢

The Debug.Log will run for every instance, if you want to see the messages on the compiled version (exe i assume) then i suggest you build a class called Debug_UI and it's sole purpose is to display all the messages from Debug.Log into the OnGui method. Debug.Log将为每个实例运行,如果您想查看编译版本上的消息(我假设是exe),那么我建议您构建一个名为Debug_UI的类,其唯一目的是显示Debug.Log中的所有消息。 OnGui方法。 First call a static fuction with the message you want to log and that function will call Debug.Log and also insert that log into a static List that will be used to display those messages on the OnGui. 首先使用要记录的消息调用静态函数,该函数将调用Debug.Log,还将该日志插入到静态列表中,该列表将用于在OnGui上显示这些消息。

// Static Utilities Class with the DebugMessage Function //带有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 Class // 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;
}

// How you would use it in your code //您将如何在代码中使用它

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


        }

and as for the messages that are called more than once you should check that you're not implementing the OnExtensionResponse method in other classes or that this class is not attached to more objects in the hierarchy. 对于多次调用的消息,应检查是否在其他类中未实现OnExtensionResponse方法,或者该类未附加到层次结构中的更多对象。

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

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