feat(NetworkManager):Allow host client to start late or rejoin

This commit is contained in:
MrGadget1024 2023-11-22 19:22:47 -05:00
parent f83d568e60
commit a4c31e7c49
3 changed files with 27 additions and 7 deletions

View File

@ -707,9 +707,10 @@ internal void OnStopServer()
} }
} }
bool clientStarted; public bool clientStarted;
internal void OnStartClient() internal void OnStartClient()
{ {
//Debug.Log($"clientStarted {connectionToClient} {clientStarted}");
if (clientStarted) return; if (clientStarted) return;
clientStarted = true; clientStarted = true;

View File

@ -404,6 +404,19 @@ public void StartClient()
return; return;
} }
if (string.IsNullOrWhiteSpace(networkAddress))
{
Debug.LogError("Must set the Network Address field in the manager");
return;
}
if (mode == NetworkManagerMode.ServerOnly)
{
mode = NetworkManagerMode.Host;
FinishStartHost();
return;
}
else
mode = NetworkManagerMode.ClientOnly; mode = NetworkManagerMode.ClientOnly;
SetupClient(); SetupClient();
@ -413,11 +426,6 @@ public void StartClient()
RegisterClientMessages(); RegisterClientMessages();
if (string.IsNullOrWhiteSpace(networkAddress))
{
Debug.LogError("Must set the Network Address field in the manager");
return;
}
// Debug.Log($"NetworkManager StartClient address:{networkAddress}"); // Debug.Log($"NetworkManager StartClient address:{networkAddress}");
NetworkClient.Connect(networkAddress); NetworkClient.Connect(networkAddress);
@ -1277,6 +1285,9 @@ void OnClientDisconnectInternal()
// shutdown client // shutdown client
NetworkClient.Shutdown(); NetworkClient.Shutdown();
foreach (NetworkIdentity identity in NetworkServer.spawned.Values)
identity.clientStarted = false;
// Exit here if we're now in ServerOnly mode (StopClient called in Host mode). // Exit here if we're now in ServerOnly mode (StopClient called in Host mode).
if (mode == NetworkManagerMode.ServerOnly) return; if (mode == NetworkManagerMode.ServerOnly) return;

View File

@ -150,9 +150,17 @@ void StopButtons()
} }
else if (NetworkServer.active) else if (NetworkServer.active)
{ {
GUILayout.BeginHorizontal();
// stop server if server-only // stop server if server-only
if (GUILayout.Button("Stop Server")) if (GUILayout.Button("Stop Server"))
manager.StopServer(); manager.StopServer();
// start client if server-only, engaging host mode.
if (GUILayout.Button("Start Client"))
manager.StartClient();
GUILayout.EndHorizontal();
} }
} }
} }