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
}
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()
/// </summary>
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>
/// Obsolete: Use <see cref="NetworkClient"/> directly
/// <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>
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()
/// </summary>
public void StartClient()
{
mode = NetworkManagerMode.ClientOnly;
InitializeSingleton();
if (authenticator != null)
@ -385,6 +399,8 @@ public void StartClient()
/// <param name="uri">location of the server to connect to</param>
public void StartClient(Uri uri)
{
mode = NetworkManagerMode.ClientOnly;
InitializeSingleton();
if (authenticator != null)
@ -455,6 +471,8 @@ void FinishStartHost()
/// </summary>
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);