简体   繁体   中英

SmartFoxServer with Unity Logging issue

I have a question about how Debug.Log works in Unity, with 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). So, I use 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?

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. I can debug my Java extension and the break point is only hit once.

Here is some example Unity C# code:

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

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. 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.

// Static Utilities Class with the DebugMessage Function

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;
}

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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