fix(Additive Levels Example): Simplified Portal::SendPlayerToNewScene

This commit is contained in:
MrGadget 2024-09-30 14:59:47 -04:00
parent c1b5053004
commit 80df4d66b6

View File

@ -27,7 +27,7 @@ public void OnLabelTextChanged(string _, string newValue)
public override void OnStartServer() public override void OnStartServer()
{ {
labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels",""); labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels", "");
// Simple Regex to insert spaces before capitals, numbers // Simple Regex to insert spaces before capitals, numbers
labelText = Regex.Replace(labelText, @"\B[A-Z0-9]+", " $0"); labelText = Regex.Replace(labelText, @"\B[A-Z0-9]+", " $0");
@ -60,42 +60,41 @@ void OnTriggerEnter(Collider other)
[ServerCallback] [ServerCallback]
IEnumerator SendPlayerToNewScene(GameObject player) IEnumerator SendPlayerToNewScene(GameObject player)
{ {
if (player.TryGetComponent(out NetworkIdentity identity)) if (!player.TryGetComponent(out NetworkIdentity identity)) yield break;
{
NetworkConnectionToClient conn = identity.connectionToClient;
if (conn == null) yield break;
// Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene). NetworkConnectionToClient conn = identity.connectionToClient;
conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true }); if (conn == null) yield break;
// wait for fader to complete. // Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene).
yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime()); conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true });
// Remove player after fader has completed // wait for fader to complete.
NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn); yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime());
// reposition player on server and client // Remove player after fader has completed
player.transform.position = startPosition; NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn);
// Rotate player to face center of scene // reposition player on server and client
// Player is 2m tall with pivot at 0,1,0 so we need to look at player.transform.position = startPosition;
// 1m height to not tilt the player down to look at origin
player.transform.LookAt(Vector3.up);
// Move player to new subscene. // Rotate player to face center of scene
SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene)); // Player is 2m tall with pivot at 0,1,0 so we need to look at
// 1m height to not tilt the player down to look at origin
player.transform.LookAt(Vector3.up);
// Tell client to load the new subscene with custom handling (see NetworkManager::OnClientChangeScene). // Move player to new subscene.
conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = true }); SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene));
// Player will be spawned after destination scene is loaded // Tell client to load the new subscene with custom handling (see NetworkManager::OnClientChangeScene).
NetworkServer.AddPlayerForConnection(conn, player); conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = true });
// host client playerController would have been disabled by OnTriggerEnter above // Player will be spawned after destination scene is loaded
// Remote client players are respawned with playerController already enabled NetworkServer.AddPlayerForConnection(conn, player);
if (NetworkClient.localPlayer != null && NetworkClient.localPlayer.TryGetComponent(out Common.Controllers.Player.PlayerControllerBase playerController))
playerController.enabled = true; // host client playerController would have been disabled by OnTriggerEnter above
} // Remote client players are respawned with playerController already enabled
if (NetworkClient.localPlayer != null && NetworkClient.localPlayer.TryGetComponent(out Common.Controllers.Player.PlayerControllerBase playerController))
playerController.enabled = true;
} }
} }
} }