Search Unity

NetworkManager scene slots select the wrong scene

Discussion in 'Multiplayer' started by Jeff-Rosenberg, Oct 2, 2016.

  1. Jeff-Rosenberg

    Jeff-Rosenberg

    Joined:
    Oct 23, 2012
    Posts:
    26


    Because of the way the NetworkManager's editor is written it will select the wrong scene in these two highlighted slots under certain conditions:
    1. The desired scene's name is a substring of another scene
    2. The desired scene is loaded after the the other scene
    For example, if I want to select a scene called "Lobby" but have another scene loaded earlier called "Test Lobby", the Test Lobby scene will be selected whenever I choose the Lobby scene.



    This will even select the wrong scene if you have a scene located in an offending folder and loaded first, for example:

    /Assets/
    /Assets/Scenes/
    /Assets/Scenes/Lobby Scenes/
    /Assets/Scenes/Lobby Scenes/Some Unrelated Scene Loaded First.unity
    /Assets/Scenes/Lobby Scenes/Lobby.unity
    This is because in UnityEditor.Networking.NetworkManagerEditor the GetSceneObject method (line 8) only checks if the desired scene's name appears somewhere in a SceneAsset's path before selecting it:

    Code (CSharp):
    1. // In UnityEditor.Networking.NetworkManagerEditor
    2. protected SceneAsset GetSceneObject(string sceneObjectName)
    3. {
    4.   if (string.IsNullOrEmpty(sceneObjectName))
    5.     return (SceneAsset) null;
    6.   foreach (EditorBuildSettingsScene scene in EditorBuildSettings.get_scenes())
    7.   {
    8.     if (scene.get_path().IndexOf(sceneObjectName) != -1) // ← This is wrong
    9.       return AssetDatabase.LoadAssetAtPath(scene.get_path(), typeof (SceneAsset)) as SceneAsset;
    10.   }
    11.   if (LogFilter.logWarn)
    12.     Debug.LogWarning((object) ("Scene [" + sceneObjectName + "] cannot be used with networking. Add this scene to the 'Scenes in the Build' in build settings."));
    13.   return (SceneAsset) null;
    14. }
    The line should be something like:

    Code (CSharp):
    1. if (scene.path.EndsWith("/" + sceneObjectName + ".unity"))
    Attached is a fixed version of NetworkManagerEditor and NetworkLobbyManagerEditor. To use them, drop them into your Editor folder. Unity isn't overriding the editors for some reason, so as a workaround you can declare a new editor for your derived NetworkManager classes. For example, my GametypeLobbyManager inherits from Unity's Prototype.NetworkLobby.LobbyManager, which inherits from NetworkLobbyManager. My editor declaration looks like this:

    Code (CSharp):
    1. // In CustomNetworkManagerEditors.cs:
    2. namespace UnityEditor
    3. {
    4.     [CustomEditor(typeof(GametypeLobbyManager))]
    5.     public class GametypeLobbyManagerEditor : NetworkLobbyManagerEditor { }
    6. }
     

    Attached Files:

    Last edited: Oct 3, 2016