- Home /
Add scenes to build through script?
The title says it all. Is there anyway i can add scenes to build settings through scripts?
I don't know the answer, but I would like to know why you need to do such thing
Hey guys, i solved the problem and thanks for replying. I was actually building a level editor for my game and was worried how can a user post their scene to the server and will it be possible to open a scene from server? The solution is ins$$anonymous$$d of posting scene , a user can post the game objects under an xml file i guess(i mean do the serialization process to save the required data). But still thank you for responding, really appreciate it
Answer by Jessespike · Jul 07, 2016 at 05:37 PM
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.
I cannot find any methods in the new EditorScene$$anonymous$$anager class that allow you to modify the build setting scenes. If you could point one out, that would be great, but otherwise, I think your initial answer of using EditorBuildSettings is still the only way to do this.
Answer by chomps32 · Jul 08, 2016 at 10:30 AM
@Jessespike that class is depreciated since 5.3,
Use EditorSceneManager:
http://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager.html
Answer by gilley033 · Oct 31, 2017 at 04:22 PM
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[i].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[i] = EditorBuildSettings.scenes[i];
newScenes[i] = 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[i];
}
newScenes[j] = new EditorBuildSettingsScene(pathOfSceneToAdd, true);
}
EditorBuildSettings.scenes = newScenes;
}
Your answer
Follow this Question
Related Questions
Unity Scene File in Build Question 1 Answer
Manually Edit EditoBuildSettings.asset 0 Answers
Scene could not be loaded even though it is in build settings 0 Answers
Create scene and add to build by script 2 Answers
How do I remove a scene from a build 1 Answer