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");
foreach (string sceneName in subScenes)
if (SceneManager.GetSceneByName(sceneName).IsValid())
if (SceneManager.GetSceneByName(sceneName).IsValid() || SceneManager.GetSceneByPath(sceneName).IsValid())
{
yield return SceneManager.UnloadSceneAsync(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);
break;
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);
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;
}
break;
case SceneOperation.UnloadAdditive:
if (SceneManager.GetSceneByName(newSceneName).IsValid())
{
if (SceneManager.GetSceneByName(newSceneName) != null)
// 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).IsValid() || SceneManager.GetSceneByPath(newSceneName).IsValid())
loadingSceneAsync = SceneManager.UnloadSceneAsync(newSceneName, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
else
Transport.activeTransport.enabled = true;
}
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;
}
break;