- Home /
Prevent NetworkLobbyManager from changing scenes.
Hello all,
Using the NetworkLobbyManager, lobbyScene and playScene must be populated to avoid errors.
If a game has been started and all clients are in the playScene, and a host quits or disconnects, then I'd like the clients to be redirected to a custom "Scores" scene.
Currently, when a host disconnects, the default NetworkLobbyManager behavior is to send the clients back to the lobby (which they cannot join as the host is gone).
Since this behavior, to me, does not make sense, I'd like to change it.
Part of the problem is that I also attempt to cleanup the NetworkManager (called Shutdown() then Destroy on the gameObject), otherwise subsequent matches throw all sorts of weird errors.
tl;dr: Is there a hook or someway I can change the default "switch to lobby on disconnect" behavior of UNET?
Thanks for your time.
Answer by Elethier · Nov 07, 2016 at 08:06 AM
The short answer is that there's no public hook within NetworkManager to stop the scene changing.
The good news is that there IS a workaround! The source code for NetworkManager is up on Bitbucket. Here's the version for the 5.4 branch. If you look at the source code, you can see that there are two spots where the scene manager is invoked:
The virtual function ServerChangeScene
The internal function ClientChangeScene
ClientChangeScene is called when the property offlineScene is set to anything besides empty string. It's also called when receiving a scene change message from the server with the name of the lobby scene. So if you subclass NetworkManager and make some adjustments, you won't change scenes no matter what connection events are occuring:
using UnityEngine.Networking;
public class MyNetworkingManager: NetworkingManager
{
private string originalLobbyScene;
public MyNetworkingManager(): base()
{
offlineScene = "";
lobbyScene = "myLobby"; // Name of the scene with the network manager
}
public override void ServerChangeScene(string sceneName)
{
// Do nothing
}
void Start()
{
originalLobbyScene = lobbyScene;
}
public override void OnStartClient(NetworkClient lobbyClient)
{
lobbyScene = originalLobbyScene; // Ensures the client loads correctly
}
public override void OnStopClient()
{
lobbyScene = ""; // Ensures we don't reload the scene after quitting
}
public override void OnStartServer()
{
lobbyScene = originalLobbyScene; // Ensures the server loads correctly
}
public override void OnStopServer()
{
lobbyScene = ""; // Ensures we don't reload the scene after quitting
}
}
(I should note, I suspect that putting offlineScene = ""; in the Start() function instead of the constructor will work, too. And I didn't run this code through a compiler, so let me know if there's any errors.)
With this solution you shouldn't see any scene changes occurring, at least for the version of the NetworkManager as of this writing. I'm using it in my game, and I haven't noticed any issues yet. If you want to use NetworkLobbyManager, just subclass NetworkLobbyManager instead of NetworkManager.
Answer by trojaon · Mar 08, 2018 at 01:49 PM
I have experienced the same issue using LobbyNetworkManager. My workaround solution was pretty much the same as Elethiers with the difference in:
public override void OnStartServer()
{
lobbyScene = originalLobbyScene; // Ensures the server loads correctly
base.OnStartServer();
}
public override void OnStopServer()
{
base.OnStartServer();
lobbyScene = ""; // Ensures we don't reload the scene after quitting
}