Created
September 26, 2017 12:21
-
-
Save yagero/2cd50a12fcc928a6446539119741a343 to your computer and use it in GitHub Desktop.
Unity: How to know if a Scene exists and can be loaded?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine.SceneManagement; | |
public static class UtilsScene | |
{ | |
/// <summary> | |
/// Returns true if the scene 'name' exists and is in your Build settings, false otherwise | |
/// </summary> | |
public static bool DoesSceneExist(string name) | |
{ | |
if (string.IsNullOrEmpty(name)) | |
return false; | |
for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++) | |
{ | |
var scenePath = SceneUtility.GetScenePathByBuildIndex(i); | |
var lastSlash = scenePath.LastIndexOf("/"); | |
var sceneName = scenePath.Substring(lastSlash + 1, scenePath.LastIndexOf(".") - lastSlash - 1); | |
if (string.Compare(name, sceneName, true) == 0) | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment