/// <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 (!NetworkingManager.singleton.NetworkConfig.EnableSceneSwitching) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("Scene switching is not enabled"); } return(null); } else if (isSwitching) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("Scene switch already in progress"); } return(null); } else if (!registeredSceneNames.Contains(sceneName)) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("The scene " + sceneName + " is not registered as a switchable scene."); } return(null); } SpawnManager.DestroySceneObjects(); //Destroy current scene objects before switching. currentSceneIndex = sceneNameToIndex[sceneName]; isSwitching = true; lastScene = SceneManager.GetActiveScene(); SceneSwitchProgress switchSceneProgress = new SceneSwitchProgress(); sceneSwitchProgresses.Add(switchSceneProgress.guid, switchSceneProgress); currentSceneSwitchProgressGuid = switchSceneProgress.guid; AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); nextScene = SceneManager.GetSceneByName(sceneName); sceneLoad.completed += (AsyncOperation AsyncOp) => { OnSceneLoaded(AsyncOp, switchSceneProgress.guid); }; switchSceneProgress.SetSceneLoadOperation(sceneLoad); using (PooledBitStream stream = PooledBitStream.Get()) { using (PooledBitWriter writer = PooledBitWriter.Get(stream)) { writer.WriteUInt32Packed(sceneNameToIndex[sceneName]); writer.WriteByteArray(switchSceneProgress.guid.ToByteArray()); InternalMessageHandler.Send(MLAPIConstants.MLAPI_SWITCH_SCENE, "MLAPI_INTERNAL", stream, SecuritySendFlags.None); } } return(switchSceneProgress); }
/// <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 (isSwitching) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("Scene switch already in progress"); } return(null); } else if (!registeredSceneNames.Contains(sceneName)) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("The scene " + sceneName + " is not registered as a switchable scene."); } return(null); } SpawnManager.ServerDestroySpawnedSceneObjects(); //Destroy current scene objects before switching. currentSceneIndex = sceneNameToIndex[sceneName]; isSwitching = true; lastScene = SceneManager.GetActiveScene(); SceneSwitchProgress switchSceneProgress = new SceneSwitchProgress(); sceneSwitchProgresses.Add(switchSceneProgress.guid, switchSceneProgress); currentSceneSwitchProgressGuid = switchSceneProgress.guid; Scene temporaryScene = SceneManager.CreateScene("MLAPI_tmp_switch_" + currentSceneSwitchProgressGuid); // Move ALL networked objects to the temp scene MoveAllNetworkedObjectsToScene(temporaryScene); AsyncOperation unloadCurrentScene = SceneManager.UnloadSceneAsync(lastScene); unloadCurrentScene.completed += (AsyncOperation asyncOp1) => { // Switch scene AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); nextScene = SceneManager.GetSceneByName(sceneName); sceneLoad.completed += (AsyncOperation asyncOp2) => { OnSceneLoaded(switchSceneProgress.guid, temporaryScene, null); }; switchSceneProgress.SetSceneLoadOperation(sceneLoad); }; return(switchSceneProgress); }
/// <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 (isSwitching) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("Scene switch already in progress"); } return(null); } else if (!registeredSceneNames.Contains(sceneName)) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("The scene " + sceneName + " is not registered as a switchable scene."); } return(null); } SpawnManager.ServerDestroySpawnedSceneObjects(); //Destroy current scene objects before switching. currentSceneIndex = sceneNameToIndex[sceneName]; isSwitching = true; lastScene = SceneManager.GetActiveScene(); SceneSwitchProgress switchSceneProgress = new SceneSwitchProgress(); sceneSwitchProgresses.Add(switchSceneProgress.guid, switchSceneProgress); currentSceneSwitchProgressGuid = switchSceneProgress.guid; AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); nextScene = SceneManager.GetSceneByName(sceneName); sceneLoad.completed += (AsyncOperation AsyncOp) => { OnSceneLoaded(AsyncOp, switchSceneProgress.guid, null); }; switchSceneProgress.SetSceneLoadOperation(sceneLoad); return(switchSceneProgress); }