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;
}

EditorBuildSettings.scenes seems to work, although it's undocumented for Unity 5.

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.

First it greys out, then when its actually playing the triangle goes blue.

5 Answers

5

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 ) );*

}

I certainly did. Here's using that to populate a UI Dropdown. public class DropDownScenePopulator : MonoBehaviour { void Start () { var optionDataList = new List<Dropdown.OptionData>(); for(int i = 0; i < SceneManager.sceneCountInBuildSettings; ++i) { string name = System.IO.Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i)); optionDataList.Add(new Dropdown.OptionData(name)); } GetComponent<Dropdown>().ClearOptions(); GetComponent<Dropdown>().AddOptions(optionDataList); }

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.

Play windows wont open when you click play. your game simply runs in the Game window. If you arent seeing anything, try repositioning the camera.

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));*

}

hi have you put animator on the object that you drag in hierarchy ?

Ok, thanks guys. Ill get on it and see how it goes, thanks a lot for taking the time to respond to my issue. Ill let you all know how I get on. Thanks again.

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._

You are totally misconstrued as to what is supposed to happen there. Pressing play in your Animation import Previewer is not indication of what will happen when you click the Game Play button. You must set up the animation components to utilise the animation clips. They dont just apply automatically. Your play mode is full functional and not broken at all. The "Game" window is the actual game preview. https://unity3d.com/learn/tutorials/modules/beginner/animation/animator-scripting

The bottom left windows (Game) is where the gameplay is, where you would expect to see your animation and game play out. The window on the right is an animation preview. I have not dabbled with animations but you have to tell the game object to play the animation. Check out for more info: https://unity3d.com/learn/tutorials/topics/animation