From 80df4d66b63dbc32672a68f5ceb694b68000d63d Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:59:47 -0400 Subject: [PATCH] fix(Additive Levels Example): Simplified Portal::SendPlayerToNewScene --- .../Examples/AdditiveLevels/Scripts/Portal.cs | 55 +++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs b/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs index 77a81b372..f64427a2b 100644 --- a/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs +++ b/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs @@ -27,7 +27,7 @@ public void OnLabelTextChanged(string _, string newValue) public override void OnStartServer() { - labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels",""); + labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels", ""); // Simple Regex to insert spaces before capitals, numbers labelText = Regex.Replace(labelText, @"\B[A-Z0-9]+", " $0"); @@ -60,42 +60,41 @@ void OnTriggerEnter(Collider other) [ServerCallback] IEnumerator SendPlayerToNewScene(GameObject player) { - if (player.TryGetComponent(out NetworkIdentity identity)) - { - NetworkConnectionToClient conn = identity.connectionToClient; - if (conn == null) yield break; + if (!player.TryGetComponent(out NetworkIdentity identity)) yield break; - // Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene). - conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true }); + NetworkConnectionToClient conn = identity.connectionToClient; + if (conn == null) yield break; - // wait for fader to complete. - yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime()); + // Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene). + conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true }); - // Remove player after fader has completed - NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn); + // wait for fader to complete. + yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime()); - // reposition player on server and client - player.transform.position = startPosition; + // Remove player after fader has completed + NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn); - // Rotate player to face center of scene - // 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); + // reposition player on server and client + player.transform.position = startPosition; - // Move player to new subscene. - SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene)); + // Rotate player to face center of scene + // 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). - conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = true }); + // Move player to new subscene. + SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene)); - // Player will be spawned after destination scene is loaded - NetworkServer.AddPlayerForConnection(conn, player); + // Tell client to load the new subscene with custom handling (see NetworkManager::OnClientChangeScene). + conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = 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; - } + // Player will be spawned after destination scene is loaded + NetworkServer.AddPlayerForConnection(conn, player); + + // 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; } } }