From 1fe1ca730cf039eba9d3ac3db4779e12e517d7a6 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 5 Jan 2020 13:10:28 +0100 Subject: [PATCH] NetworkManager.mode to simplify FinishLoadScene afterwards --- Assets/Mirror/Runtime/NetworkManager.cs | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 44697973e..b6d44308b 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -18,6 +18,8 @@ public enum PlayerSpawnMethod RoundRobin } + public enum NetworkManagerMode { Offline, ServerOnly, ClientOnly, Host } + [AddComponentMenu("Network/NetworkManager")] [HelpURL("https://mirror-networking.com/docs/Components/NetworkManager.html")] public class NetworkManager : MonoBehaviour @@ -174,6 +176,14 @@ public static bool IsHeadless() /// public static bool isHeadless => SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null; + // helper enum to know if we started the networkmanager as server/client/host. + // -> this is necessary because when StartHost changes server scene to + // online scene, FinishLoadScene is called and the host client isn't + // connected yet (no need to connect it before server was fully set up). + // in other words, we need this to know which mode we are running in + // during FinishLoadScene. + public NetworkManagerMode mode { get; private set; } + /// /// Obsolete: Use directly /// For example, use NetworkClient.Send(message) instead of NetworkManager.client.Send(message) @@ -315,6 +325,8 @@ void SetupServer() /// public void StartServer() { + mode = NetworkManagerMode.ServerOnly; + // StartServer is inherently ASYNCHRONOUS (=doesn't finish immediately) // // Here is what it does: @@ -351,6 +363,8 @@ public void StartServer() /// public void StartClient() { + mode = NetworkManagerMode.ClientOnly; + InitializeSingleton(); if (authenticator != null) @@ -385,6 +399,8 @@ public void StartClient() /// location of the server to connect to public void StartClient(Uri uri) { + mode = NetworkManagerMode.ClientOnly; + InitializeSingleton(); if (authenticator != null) @@ -455,6 +471,8 @@ void FinishStartHost() /// public virtual void StartHost() { + mode = NetworkManagerMode.Host; + // StartHost is inherently ASYNCHRONOUS (=doesn't finish immediately) // // Here is what it does: @@ -537,6 +555,10 @@ public void StopHost() { OnStopHost(); + // set offline mode BEFORE changing scene so that FinishStartScene + // doesn't think we need initialize anything. + mode = NetworkManagerMode.Offline; + StopServer(); StopClient(); } @@ -557,6 +579,11 @@ public void StopServer() if (LogFilter.Debug) Debug.Log("NetworkManager StopServer"); isNetworkActive = false; NetworkServer.Shutdown(); + + // set offline mode BEFORE changing scene so that FinishStartScene + // doesn't think we need initialize anything. + mode = NetworkManagerMode.Offline; + if (!string.IsNullOrEmpty(offlineScene)) { ServerChangeScene(offlineScene); @@ -583,6 +610,10 @@ public void StopClient() NetworkClient.Disconnect(); NetworkClient.Shutdown(); + // set offline mode BEFORE changing scene so that FinishStartScene + // doesn't think we need initialize anything. + mode = NetworkManagerMode.Offline; + if (!string.IsNullOrEmpty(offlineScene) && SceneManager.GetActiveScene().name != offlineScene) { ClientChangeScene(offlineScene, SceneOperation.Normal);