fix: Check SceneManager GetSceneByName and GetSceneByPath (#1684)

* fix: Check SceneManager GetSceneByName and GetSceneByPath

* Fix example too.
This commit is contained in:
MrGadget 2020-04-10 12:26:37 -04:00 committed by GitHub
parent 55e075c872
commit e7cfd5a498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 11 deletions

View File

@ -45,7 +45,7 @@ IEnumerator UnloadScenes()
if (LogFilter.Debug) Debug.Log("Unloading Subscenes"); if (LogFilter.Debug) Debug.Log("Unloading Subscenes");
foreach (string sceneName in subScenes) foreach (string sceneName in subScenes)
if (SceneManager.GetSceneByName(sceneName).IsValid()) if (SceneManager.GetSceneByName(sceneName).IsValid() || SceneManager.GetSceneByPath(sceneName).IsValid())
{ {
yield return SceneManager.UnloadSceneAsync(sceneName); yield return SceneManager.UnloadSceneAsync(sceneName);
if (LogFilter.Debug) Debug.Log($"Unloaded {sceneName}"); if (LogFilter.Debug) Debug.Log($"Unloaded {sceneName}");

View File

@ -850,25 +850,28 @@ internal void ClientChangeScene(string newSceneName, SceneOperation sceneOperati
loadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName); loadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName);
break; break;
case SceneOperation.LoadAdditive: case SceneOperation.LoadAdditive:
if (!SceneManager.GetSceneByName(newSceneName).IsValid()) // Ensure additive scene is not already loaded on client by name or path
// since we don't know which was passed in the Scene message
if (!SceneManager.GetSceneByName(newSceneName).IsValid() && !SceneManager.GetSceneByPath(newSceneName).IsValid())
loadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName, LoadSceneMode.Additive); loadingSceneAsync = SceneManager.LoadSceneAsync(newSceneName, LoadSceneMode.Additive);
else else
{ {
Debug.LogWarningFormat("Scene {0} is already loaded", newSceneName); Debug.LogWarning($"Scene {newSceneName} is already loaded");
// Re-enable the transport that we disabled before entering this switch
Transport.activeTransport.enabled = true; Transport.activeTransport.enabled = true;
} }
break; break;
case SceneOperation.UnloadAdditive: case SceneOperation.UnloadAdditive:
if (SceneManager.GetSceneByName(newSceneName).IsValid()) // Ensure additive scene is actually loaded on client by name or path
{ // since we don't know which was passed in the Scene message
if (SceneManager.GetSceneByName(newSceneName) != null) if (SceneManager.GetSceneByName(newSceneName).IsValid() || SceneManager.GetSceneByPath(newSceneName).IsValid())
loadingSceneAsync = SceneManager.UnloadSceneAsync(newSceneName, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects); loadingSceneAsync = SceneManager.UnloadSceneAsync(newSceneName, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
else
Transport.activeTransport.enabled = true;
}
else else
{ {
Debug.LogWarning("Cannot unload the active scene with UnloadAdditive operation"); Debug.LogWarning($"Cannot unload {newSceneName} with UnloadAdditive operation");
// Re-enable the transport that we disabled before entering this switch
Transport.activeTransport.enabled = true; Transport.activeTransport.enabled = true;
} }
break; break;