NetworkManager.mode to simplify FinishLoadScene afterwards

This commit is contained in:
vis2k 2020-01-05 13:10:28 +01:00
parent febcd6f37f
commit 1fe1ca730c

View File

@ -18,6 +18,8 @@ public enum PlayerSpawnMethod
RoundRobin RoundRobin
} }
public enum NetworkManagerMode { Offline, ServerOnly, ClientOnly, Host }
[AddComponentMenu("Network/NetworkManager")] [AddComponentMenu("Network/NetworkManager")]
[HelpURL("https://mirror-networking.com/docs/Components/NetworkManager.html")] [HelpURL("https://mirror-networking.com/docs/Components/NetworkManager.html")]
public class NetworkManager : MonoBehaviour public class NetworkManager : MonoBehaviour
@ -174,6 +176,14 @@ public static bool IsHeadless()
/// </summary> /// </summary>
public static bool isHeadless => SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null; 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; }
/// <summary> /// <summary>
/// Obsolete: Use <see cref="NetworkClient"/> directly /// Obsolete: Use <see cref="NetworkClient"/> directly
/// <para>For example, use <c>NetworkClient.Send(message)</c> instead of <c>NetworkManager.client.Send(message)</c></para> /// <para>For example, use <c>NetworkClient.Send(message)</c> instead of <c>NetworkManager.client.Send(message)</c></para>
@ -315,6 +325,8 @@ void SetupServer()
/// <returns></returns> /// <returns></returns>
public void StartServer() public void StartServer()
{ {
mode = NetworkManagerMode.ServerOnly;
// StartServer is inherently ASYNCHRONOUS (=doesn't finish immediately) // StartServer is inherently ASYNCHRONOUS (=doesn't finish immediately)
// //
// Here is what it does: // Here is what it does:
@ -351,6 +363,8 @@ public void StartServer()
/// </summary> /// </summary>
public void StartClient() public void StartClient()
{ {
mode = NetworkManagerMode.ClientOnly;
InitializeSingleton(); InitializeSingleton();
if (authenticator != null) if (authenticator != null)
@ -385,6 +399,8 @@ public void StartClient()
/// <param name="uri">location of the server to connect to</param> /// <param name="uri">location of the server to connect to</param>
public void StartClient(Uri uri) public void StartClient(Uri uri)
{ {
mode = NetworkManagerMode.ClientOnly;
InitializeSingleton(); InitializeSingleton();
if (authenticator != null) if (authenticator != null)
@ -455,6 +471,8 @@ void FinishStartHost()
/// </summary> /// </summary>
public virtual void StartHost() public virtual void StartHost()
{ {
mode = NetworkManagerMode.Host;
// StartHost is inherently ASYNCHRONOUS (=doesn't finish immediately) // StartHost is inherently ASYNCHRONOUS (=doesn't finish immediately)
// //
// Here is what it does: // Here is what it does:
@ -537,6 +555,10 @@ public void StopHost()
{ {
OnStopHost(); OnStopHost();
// set offline mode BEFORE changing scene so that FinishStartScene
// doesn't think we need initialize anything.
mode = NetworkManagerMode.Offline;
StopServer(); StopServer();
StopClient(); StopClient();
} }
@ -557,6 +579,11 @@ public void StopServer()
if (LogFilter.Debug) Debug.Log("NetworkManager StopServer"); if (LogFilter.Debug) Debug.Log("NetworkManager StopServer");
isNetworkActive = false; isNetworkActive = false;
NetworkServer.Shutdown(); 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)) if (!string.IsNullOrEmpty(offlineScene))
{ {
ServerChangeScene(offlineScene); ServerChangeScene(offlineScene);
@ -583,6 +610,10 @@ public void StopClient()
NetworkClient.Disconnect(); NetworkClient.Disconnect();
NetworkClient.Shutdown(); 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) if (!string.IsNullOrEmpty(offlineScene) && SceneManager.GetActiveScene().name != offlineScene)
{ {
ClientChangeScene(offlineScene, SceneOperation.Normal); ClientChangeScene(offlineScene, SceneOperation.Normal);