- Home /
SceneManager.GetAllScenes() only returns the current scene
I've got a project where I simply want to put a scene menu. Here's the code:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class PickScene : MonoBehaviour {
Scene[] Scenes;
// Use this for initialization
void Start () {
Scenes = SceneManager.GetAllScenes();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
Debug.Log(SceneManager.GetAllScenes().Length);
for (int i = 0; i < Scenes.Length; i++)
{
if (GUI.Button(new Rect(10, i*50,300,45), Scenes[i].name))
{
SceneManager.LoadScene(i);
}
}
}
}
Works as expected, with no errors, but it only puts up a button for the Current scene, the Menu. There are two other scenes in the Build Settings, but they don't show up, so I can't test if this is working. The Debug shows 1 Scene. Any idea why this wouldn't show me how many scenes there are?
The documentation on this is not complete, so I have nowhere else to turn.
I just found sceneCountInBuildSettings(), and that works, giving me the correct number of scenes, but I still do not see a way to iterate through the Build Settings and get the list of scenes. Am I missing something?
I'm having the same issue. I was under the impression GetAllScenes() would return all the scenes in build settings.
I think it's a bug, so I reported it as a bug. Just not sure what the difference would be between sceneCount and sceneCountInBuildSettings. I'd think they'd be the same.
Ya, getSceneCountInBuildSettings returns the right number, but when I load a scene based off of the scene count it says the scene doesn't exist
Thank you for posting this answer. It solved my issue of how to replace the deprecated Application.levelCount with more contemporary code.
@Leuthil Oh it 100% does give the count and list of loaded scenes only. In my scene menu app, clicking on the only button it made, the menu, reloaded the scene and increased the count of sceneCount by one each time.
But who wants the list of loaded scenes? I'd think it makes more sense to give us the list of scenes in the build settings, and then let us iterate that for whether isLoaded, or have a subset of that which a function like GetLoadedScenes() would return. GetAllScenes() should do exactly that.
I tried to loop through and load all the scenes to see if they'd show up in the list, but it actually loads them and makes them active. Yeah, I could do an asynchronous load, but then I'm loading every scene just to get the list of scenes so I can load the scene I want?
Nah, seems like we need GetLoadedScenes() and GetAllScenes(), or GetScenesInBuildSettings() (and an explanation of what the point of GetAllScenes() is for.)
$$anonymous$$y program is a demo of Cardboard that contains several different scenes, so I'd really like to be able to just DYNA$$anonymous$$ICALLY get a list of scenes, make buttons, and load the scene they choose. I do it now by manually creating the buttons for each scene in OnGUI(), but then I have to edit this script each time I add a new scene.
I agree. If there is a sceneCount and a sceneCountInBuildSettings, there should be a GetAllScenes() and a GetAllScenesInBuildSettings() or something like that.
Yeah this totally irks me. $$anonymous$$y game is a online game with multiple levels for online play - I want to allow the levels to cycle, and for players to vote on the level they want next. It would be nice if my list of build settings game levels were automatically added to a list.
Until they give us a GetScenesInBuildSettings(), you can get a list of build settings scenes using EditorBuildSettings.scenes http://docs.unity3d.com/412/Documentation/ScriptReference/EditorBuildSettings-scenes.html
The catch is it's UnityEditor only. But you can create a custom inspector that saves it to a $$anonymous$$onobehaviour list.
They are ordered, so remove the non-enabled scenes and you can get the buildIndex. and to get the name, use Path.GetFileNameWithoutExtension(scenePathFromBuildSettings);
@FlyingHighUp Now that's an interesting solution! I think I can use pre-compiler directives so that in the Editor, it keeps the list up to date, and then in standalone mode, it just reads the list of scenes.
I'd call that a solution. Please change your comment to a reply so I can mark this as a solution and give you props.
Answer by FlyingHighUp · Dec 23, 2015 at 05:42 PM
Until they give us a GetScenesInBuildSettings(), you can get a list of build settings scenes using EditorBuildSettings.scenes http://docs.unity3d.com/412/Documentation/ScriptReference/EditorBuildSettings-scenes.html
The catch is it's UnityEditor only. But you can create a custom inspector that saves it to a list in some Monobehaviour.
They are ordered, so remove the non-enabled scenes and you can get the buildIndex. To get the scene name, use Path.GetFileNameWithoutExtension(scenePathFromBuildSettings);
Well, this worked. I've got a nice menu interface that gathers the list of scenes in editor mode, and then makes buttons that allow you to select the level. Works on the cell phone. NOW I have a cardboard demo! Thanks!
Answer by manelroure · Mar 21, 2016 at 11:27 PM
I had the same problem and with the help of this post I could fix it. I think this code could help those who want to solve the same problem.
Thanks! :)
using UnityEditor;
using System.IO;
EditorBuildSettingsScene[] allScenes = EditorBuildSettings.scenes;
Debug.Log ("All Scenes : Length : "+allScenes.Length);
string path;
for (int i = 0; i < allScenes.Length; i++) {
Debug.Log ("All Path : Scene : "+allScenes[i].path);
path = Path.GetFileNameWithoutExtension (allScenes[i].path);
Debug.Log ("Clear Path : Scene : "+path);
}
Fair warning for anyone who's looking to export their game in the future: This code is not for you!
According to the current best answer from FlyingHighUp EditorBuildSettings.scenes only works in the Editor, therefore preventing you from building any projects you use this type of code in.
Answer by Quatum1000 · Jul 22, 2019 at 12:29 AM
The confusing thing is, there is a EditorSceneManager and a SceneManager. The SceneManager works also in the Editor as well.
This example fills a scenes array with all scenes in the hierarchy.
using UnityEngine.SceneManagement;
Scene[] Scenes = new Scene[SceneManager.sceneCount];
for (int i = 0; i < SceneManager.sceneCount; i++) {
Scenes[i] = SceneManager.GetSceneAt(i);
Debug.Log(Scenes[i].name);
}
Your answer
Follow this Question
Related Questions
UnloadSceneAsync() does not seem to work with additive scenes. 1 Answer
Most efficient way to transition between scenes? 1 Answer
SceneManager.LoadScene not working with button 1 Answer
Trying to access SceneManagement 1 Answer
How to ensure that an additively loaded scene is active before it becomes interactive? 1 Answer