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~~.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~~.buildIndex != -1)
{
scenesInBuild[s - indexMod] = allScenes
;
}
else
{
indexMod++;
}
}~~

return scenesInBuild;
}

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 *= System.IO.Path.GetFileNameWithoutExtension( UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex( i ) );*

}

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.

Using System.IO

        // Get build scenes
        var sceneNumber = SceneManager.sceneCountInBuildSettings;
        string[] arrayOfNames;
        arrayOfNames = new string[sceneNumber];

        for (int i = 0; i < sceneNumber; i++)
        {
            arrayOfNames *= Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));*

}

Here is a more concise way:

string[] scenes = EditorBuildSettings.scenes
            .Where( scene => scene.enabled )
            .Select( scene => scene.path )
            .ToArray();

I was needing the correct build index for an unloaded scene to use PhotonNetwork.LoadLevel but I didn’t want to hard code the index number for each scene in case I changed them in the future. Update I don’t know why I thought it would work but everything that uses UnityEditor will not build or work at runtime, so I had to change a lot.

First, add

using UnityEditor;

next, I added these fields

[SerializeField] string[] scenesPathsInBuild;
[SerializeField] bool[] scenesStatusInBuild;
private static Dictionary<string, int> _validScenes = new Dictionary<string, int>();
private static string[] _scenePaths;

then in Start(), I added:

#if UNITY_EDITOR
    private void OnValidate()
    {
        List<string> s = new List<string>();
        List<bool> b = new List<bool>();
        EditorBuildSettingsScene[] sceneData = EditorBuildSettings.scenes;
        for (int i = 0; i < sceneData.Length; i++)
        {
            s.Add(sceneData*.path);*

b.Add(sceneData*.enabled);*
}
scenesPathsInBuild = s.ToArray();
scenesStatusInBuild = b.ToArray();
}
#endif
This above method runs when you are in the editor so you will need to have opened the scene that has a reference to this script for the values to update (I think, might update from any scene, didn’t test it).
Now you need to get the SerializeFields data to the static dictionary at runtime so add this to Awake():
private void Awake()
{
List s = new List();
int buildIndex = 0;
for (int i = 0; i < scenesPathsInBuild.Length; i++)
{
if (scenesStatusInBuild*) {*
validScenes.Add(scenesPathsInBuild*, buildIndex );*
buildIndex++;
s.Add(scenesPathsInBuild*);*
}
}
_scenePaths = s.ToArray();
}
This means you can now create a static method to get the build scene index of a scene from a scene path, but since you are probably starting with a scene name and not the path we also need to check if the scene name is found in any of the scene paths.
public static int GetSceneIndexFromSceneName(string targetSceneName)
{
for (int i = 0; i < _scenePaths.Length; i++)
{
if (scenePaths*.Contains(targetSceneName))*
{

return LaunchManager.ValidScenes[scenePaths*];*
}
}
return -1;
}
This method will return the build index of the scene name that it is passed.
Here is what I call for my LoadScene Method:
PhotonNetwork.LoadLevel(GetSceneIndexFromSceneName(/string of target scene path/));
Hope this helps someone._