/// <summary> /// Switches to a scene with a given name. Can only be called from Server /// </summary> /// <param name="sceneName">The name of the scene to switch to</param> public static SceneSwitchProgress SwitchScene(string sceneName) { if (!NetworkManager.Singleton.IsServer) { throw new NotServerException("Only server can start a scene switch"); } if (s_IsSwitching) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning("Scene switch already in progress"); } return(null); } if (!RegisteredSceneNames.Contains(sceneName)) { if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { NetworkLog.LogWarning($"The scene {sceneName} is not registered as a switchable scene."); } return(null); } NetworkSpawnManager.ServerDestroySpawnedSceneObjects(); //Destroy current scene objects before switching. s_IsSwitching = true; s_LastScene = SceneManager.GetActiveScene(); var switchSceneProgress = new SceneSwitchProgress(); SceneSwitchProgresses.Add(switchSceneProgress.Guid, switchSceneProgress); CurrentSceneSwitchProgressGuid = switchSceneProgress.Guid; // Move ALL NetworkObjects to the temp scene MoveObjectsToDontDestroyOnLoad(); IsSpawnedObjectsPendingInDontDestroyOnLoad = true; // Switch scene AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single); s_NextSceneName = sceneName; sceneLoad.completed += (AsyncOperation asyncOp2) => { OnSceneLoaded(switchSceneProgress.Guid, null); }; switchSceneProgress.SetSceneLoadOperation(sceneLoad); OnSceneSwitchStarted?.Invoke(sceneLoad); return(switchSceneProgress); }