Add scenes to build through script?

The title says it all. Is there anyway i can add scenes to build settings through scripts?

Not documented very well, but should be clear enough to use. It’s an Editor script you’ll need to include UnityEditor namespace.

EditorBuildSettings.scenes

---------- edit ----------

EditorBuildSettings is deprecrated since 5.3, use EditorSceneManager instead. Thanks to chomps32 for correction.

@Jessespike that class is depreciated since 5.3,

Use EditorSceneManager:

I could not find any method to do this using the EditorSceneManager, and I don’t see anything about EditorBuildSettings being deprecated.

Here is how I do it:

void AddSceneToBuildSettings(string pathOfSceneToAdd)
{
        //Loop through and see if the scene already exist in the build settings
        int indexOfSceneIfExist = -1;

        for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
        {
            if (EditorBuildSettings.scenes*.path == pathOfSceneToAdd)*

{
indexOfSceneIfExist = i;
break;
}
}

EditorBuildSettingsScene[] newScenes;

if (indexOfSceneIfExist == -1)
{
newScenes = new EditorBuildSettingsScene[EditorBuildSettings.scenes.Length + 1];

//Seems inefficent to add scene to build settings after creating each scene (rather than doing it all at once
//after they are all created, however, it’s necessary to avoid memory issues.
int i = 0;
for (; i < EditorBuildSettings.scenes.Length; i++)
newScenes = EditorBuildSettings.scenes*;*

newScenes = new EditorBuildSettingsScene(pathOfSceneToAdd, true);
}
else
{
newScenes = new EditorBuildSettingsScene[EditorBuildSettings.scenes.Length];

int i = 0, j = 0;
for (; i < EditorBuildSettings.scenes.Length; i++)
{
//skip over the scene that is a duplicate
//this will effectively delete it from the build settings
if (i != indexOfSceneIfExist)
newScenes[j++] = EditorBuildSettings.scenes*;*
}
newScenes[j] = new EditorBuildSettingsScene(pathOfSceneToAdd, true);
}

EditorBuildSettings.scenes = newScenes;
}