- Home /
How can I get a list of all scenes in the build?
Hello, Scene Manager only has GetAllScenes, there is no GetAllScenesInBuildSettings. GetAllScenes doesn't work besides, it just returns one scene, even though I have way more. it might be because I'm using this script in the editor as part of some UI generation.
Anyways, here's a script a wrote for this, but, like I said, it says there's only one scene. Any idea how to work around this?
Scene[] getAllScenesInBuild()
{
Scene[] allScenes = SceneManager.GetAllScenes();
Debug.Log("total number of scenes: " + allScenes.Length); //this prints '1' when I call this function
int numOfScenesInBuild = 0;
for (int s = 0; s < allScenes.Length; s++) //get total number of scenes in build
{
if (allScenes[s].buildIndex != -1)
{
numOfScenesInBuild++;
}
}
Debug.Log("numOfScenesInBuild: " + numOfScenesInBuild);
Scene[] scenesInBuild = new Scene[numOfScenesInBuild];
int indexMod = 0;
for (int s = 0; s < allScenes.Length; s++) //get total number of scenes in build
{
if (allScenes[s].buildIndex != -1)
{
scenesInBuild[s - indexMod] = allScenes[s];
}
else
{
indexMod++;
}
}
return scenesInBuild;
}
EditorBuildSettings.scenes seems to work, although it's undocumented for Unity 5.
hmm, I've never heard about that, I'll give it a look
Sorry, that wont work, as EditorBuildSettings is apart of the UnityEditor dll, which is non-distrbutable for good security reasons... I think... but yea, @maccabbe. Would be nice, I think the only way truely to do this is to loop through each scene, load each scene, grab its name in a list, then unload them... seems long and boring, but you can start with an animation or something, just render it to a canvas or something that is placed really close to the camera, then LoadAsync and wait for each to finish... I will try this and maybe write something myself...
Do you need to get it by script or are you just curious how many scenes you have?
If you need it by script @yasirkula script should work.
Answer by yasirkula · May 23, 2017 at 08:21 AM
The question is sorta old but maybe this will help someone... With Unity 5.5, they've added SceneUtility:
int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings;
string[] scenes = new string[sceneCount];
for( int i = 0; i < sceneCount; i++ )
{
scenes[i] = System.IO.Path.GetFileNameWithoutExtension( UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex( i ) );
}
I certainly did. Here's using that to populate a UI Dropdown.
public class DropDownScenePopulator : $$anonymous$$onoBehaviour {
void Start () {
var optionDataList = new List<Dropdown.OptionData>();
for(int i = 0; i < Scene$$anonymous$$anager.sceneCountInBuildSettings; ++i) {
string name = System.IO.Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
optionDataList.Add(new Dropdown.OptionData(name));
}
GetComponent<Dropdown>().ClearOptions();
GetComponent<Dropdown>().AddOptions(optionDataList);
}
Answer by jj_unity328 · Aug 10, 2018 at 07:43 AM
There is a simpler solution that also let you check if the scene is enabled in the BuildSettings.
List<string> scenes = new List<string>();
foreach(EditorBuildingSettingsScene scene in EditorBuildSettings.scenes)
{
if(scene.enabled)
scenes.add(scene.path);
}
Versus UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i)
which lists all, even disabled scenes and you have no way to check their status.
The class name EditorBuildSettingsScene
is misspelled in your post. Here is the correct spelling: EditorBuildSettingsScene
.
EditorBuildSettingsScene* and not EditorBuildingSettingsScene scenes.Add* and not scenes.add*
Wouldn't recommend since you can't use EditorBuildSettings in build.
Add a scene in the build at runtime is non-sense, because if you are at runtime, that mean the scene is already in the build. And to get another scene... you would need to rebuild, right ?
It's not just for adding scenes though, in my case I made a scene menu to quickly change scenes. So it's usefull to get a list of scenes in the build settings, even in a build.
Answer by HoloLensPadawan · Sep 04, 2019 at 09:18 PM
Using System.IO
// Get build scenes
var sceneNumber = SceneManager.sceneCountInBuildSettings;
string[] arrayOfNames;
arrayOfNames = new string[sceneNumber];
for (int i = 0; i < sceneNumber; i++)
{
arrayOfNames[i] = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
}
Answer by TZ- · Mar 17, 2018 at 10:43 PM
You sir are amazing, after 2 days googling a problem I was having finally my googlefu found you, I had been trying every single suggestion I could find all to no avail.
your code works perfectly in my editor script I'm trying to make ( I say trying as I am just a learner at c# and Unity) thank you so much I was very nearly going to give up and mark it as something I would have to come back to later once I had learned more. :D
Answer by sebtoun · Oct 15, 2021 at 08:38 PM
Here is a more concise way:
string[] scenes = EditorBuildSettings.scenes
.Where( scene => scene.enabled )
.Select( scene => scene.path )
.ToArray();