Skip to content

Instantly share code, notes, and snippets.

@yagero
Created September 26, 2017 12:21
Show Gist options
  • Save yagero/2cd50a12fcc928a6446539119741a343 to your computer and use it in GitHub Desktop.
Save yagero/2cd50a12fcc928a6446539119741a343 to your computer and use it in GitHub Desktop.
Unity: How to know if a Scene exists and can be loaded?
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