- Home /
How to determine whether a Scene with 'name' exists (with the new SceneManager)?
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...
(see my answer below - I wrote a custom thing for this that I'm using!)
Answer by JustHonour · Sep 13, 2017 at 12:53 PM
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?
}
Ziplock9000 - why do you say "Do not use this, it's a terrible answer"? I haven't come across any problems using this answer myself.
Answer by HadynTheHuman · Aug 16, 2017 at 07:10 AM
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))
Why can't we just use "System.IO.Path.GetFileNameWithoutExtension(scenePath))" ins$$anonymous$$d? I tried it and seems working well.
Couldn't you just use?
if(SceneUtility.GetBuildIndexByScenePath(sceneName) >= 0)
(ps. Thank you didn't know about this before)
Dude, you do not know, but you saved my life :)
Scene indexes start with 0, your list wouldn't add the start scene to scenesInBuild. But nethertheless, thanks! Your answer helped me.
Answer by SarfaraazAlladin · Sep 25, 2016 at 05:54 PM
The short answer is that you can't.
There are some ways to work around the limitation though. Checkout this post:
http://answers.unity3d.com/questions/1115796/scenemanagergetallscenes-only-returns-the-current.html
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.
Unfortunately, it seems like EditorBuildSettings has been deprecated, so I don't think this solution would work today. Thanks anyway. I agree, there should be a very simple way to do this.
Answer by FussenKuh · Dec 01, 2016 at 10:08 AM
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.
This is the first thing I tried and no, it doesn't work. GetSceneByName will only return scenes that have already been loaded in the first place (which kind of defeats the whole purpose of IsValid, really).
$$anonymous$$aybe I'm still missing something... Are you using Unity 5.4.3f1 or an older version? I can't vouch for earlier versions, but, after downloading 5.4.3f1 earlier in the week, I wrote that function last night for my current project and it appears to give me the exact behavior I expect.
For example: I have a scene called "Loading." If I call LoadScene("Loading"), I successfully enter the block of "Load $$anonymous$$y Scene" code. On the other hand, if I call my function with an arbitrary string that does not map to a scene in my game, say LoadScene("$$anonymous$$yAwesomeSceneThatDoesNotExist"), I hit the 'else' block and receive the warning message.
If you're running 5.4.3f1 and are seeing different behavior, I'm going to label myself very confused.
Scratch that...
After poking around a bit more I now understand what the two of you are saying. I'm seeing the same (non) functionality the two of you are reporting.
I'm going to chalk my confusion up to being up too late and failing at reading comprehension coupled with a mis-configuration of the test on my part.
So, yes, GetSceneByName does, indeed, seem completely useless.
Answer by MajeureX · Jul 17, 2020 at 11:00 PM
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.
Your answer
