How can I determine whether a scene exists and can be loaded or not? It seems that SceneManager.GetSceneByName(name) only returns scenes that are loaded…
This has worked for me. Although I’m not sure if it’s ideal.
if (Application.CanStreamedLevelBeLoaded("sceneName")
{
SceneManager.LoadScene("sceneName");
}
else
{
//Go to a different scene?
}
You can use SceneUtility.GetScenePathByBuildIndex to get a list of valid scene names:
List<string> scenesInBuild = new List<string>();
for (int i = 1; i < SceneManager.sceneCountInBuildSettings; i++)
{
string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
int lastSlash = scenePath.LastIndexOf("/");
scenesInBuild.Add(scenePath.Substring(lastSlash + 1, scenePath.LastIndexOf(".") - lastSlash - 1));
}
Then check if a scene exists with:
if(scenesInBuild.Contains(sceneName))
The short answer is that you can’t.
There are some ways to work around the limitation though. Checkout this post:
It’s a shame that there isn’t an easy way to get access to the build settings yet. Until Unity addresses it, I hope that post sets you in the right direction.
I’ve created a method that uses the SceneUtility.GetBuildIndexByScenePath
method to check that a scene exists in the build path:
bool IsSceneLoadableFromPath(string scenePath)
{
if (String.IsNullOrWhiteSpace(scenePath))
return false;
if (SceneUtility.GetBuildIndexByScenePath(scenePath) >= 0)
return true;
// Allow some extra variations on scenePath since SceneManager.LoadSceneAsync is
// more relaxed about scene path
if (SceneUtility.GetBuildIndexByScenePath(scenePath + ".unity") >= 0)
return true;
if (SceneUtility.GetBuildIndexByScenePath("Assets/" + scenePath) >= 0)
return true;
if (SceneUtility.GetBuildIndexByScenePath("Assets/" + scenePath + ".unity") >= 0)
return true;
return false;
}
You’ll notice that the method makes multiple attempts to validate the given path before finally returning the result false
. I’ve implemented it like this as SceneUtility.GetBuildIndexByScenePath
method is more strict/precise in the path it expects than the SceneManager.LoadSceneAsync
: the latter method lets you omit ‘Assets’ directory and ‘.unity’ suffix. These additional checks accomodate these differences.
UPDATE - Scratch everything I’ve said below. While this should work, the OP is correct, it doesn’t… at least in 5.4.3f1 and 5.5.0f3. The quick test I ran was flawed which lead to my misunderstanding.
I’m a bit late to this party and, perhaps I’m misinterpreting the question, but, the following seems to work just fine for me in Unity 5.4.3f1:
public void LoadScene(string argScene)
{
if (UnityEngine.SceneManagement.SceneManager.GetSceneByName(argScene).IsValid())
{
< Load My Scene >
}
else
{
Debug.LogWarning("Load Scene Failed. " + argScene + " not found.");
}
}
If I attempt to load a non-existent scene, the .IsValid() function returns FALSE and I dump into my warning message block.