Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to add Scenes in Build with script with "EditorBuildSettings.scenes" ??

Discussion in 'Scripting' started by sama-van, Dec 20, 2011.

  1. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,723
    Hi!!

    Does someone know if there is a way to "drop" scenes into the Scenes In Build window from script?

    I usually drop all scenes manually before each release, but it starts to be pretty heavy to repeat the drag and drop for hundred of scenes :(

    I found the "EditorBuildSettings.scenes" function into the scripting doc, but there is not C# example :

    - http://unity3d.com/support/documentation/ScriptReference/EditorBuildSettings-scenes.html

    I really do not have any idea on how to use it and interact with Unity itself o_O.


    A quick example or URL about tool scripting for Unity would be ok to start...
    I didn't found much thing about it :(


    Thank you much! :p
     
  2. Lamont

    Lamont

    Joined:
    Dec 1, 2010
    Posts:
    114
    All done :). Check IM.
     
  3. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,723
    Thanks lamont!

    Here is the code with some customization and loop process :



    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class AddScenesToBuild : EditorWindow
    6. {  
    7.     [MenuItem ("FolioTools/AddScenesToBuild")]
    8.    
    9.     static void Init ()
    10.     {
    11.         string MainFolder   = "Assets/Application/Scenes/";
    12.         string SceneType    = ".unity";
    13.  
    14.         string [] ScenesList = new string [] {  "2006/2006_Colossus/Scene_Colossus_CH_Lycan_A",
    15.                                                 "2006/2006_Colossus/Scene_Colossus_CH_Tiamate_A",
    16.                                                 "2006/2006_Colossus/Scene_Colossus_CR_Kokopeli_A",
    17.                                                 "2006/2006_Colossus/Scene_Colossus_CR_Marmotte_A",
    18.                                                 "2006/2006_Colossus/Scene_Colossus_CR_TitanDeNuit_A",};
    19.  
    20.         EditorBuildSettingsScene[] original = EditorBuildSettings.scenes;
    21.         EditorBuildSettingsScene[] newSettings = new EditorBuildSettingsScene[original.Length + ScenesList.Length];
    22.         System.Array.Copy(original, newSettings, original.Length);
    23.        
    24.         Debug.Log ("Original Build Lenght = " + original.Length);
    25.         Debug.Log ("New Build Lenght = " + newSettings.Length);
    26.        
    27.        
    28.         int i = 0;
    29.         int index = original.Length;
    30.        
    31.         for (i = 0; i < ScenesList.Length; i ++)
    32.         {
    33.             Debug.Log ("i = " + i);
    34.             EditorBuildSettingsScene sceneToAdd = new EditorBuildSettingsScene(MainFolder + ScenesList[i] + SceneType, true);
    35.             newSettings[index] = sceneToAdd;
    36.            
    37.             index ++;
    38.         }  
    39.        
    40.         EditorBuildSettings.scenes = newSettings;
    41.     }
    42.    
    43. }
     
    havokentity, Chopium and mikaelK like this.
  4. noah_petro

    noah_petro

    Joined:
    Jun 20, 2017
    Posts:
    6
    Your code doesn't work dude
     
  5. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,723
    it's super oldish O_O.... like 6years ago... maybe a few things changed since!?....
     
    sp-LeventeLajtai and noah_petro like this.
  6. noah_petro

    noah_petro

    Joined:
    Jun 20, 2017
    Posts:
    6
    I actually got it working yesterday to fit what I wanted it to do (Thank you!), but now im getting a NullReferenceException at your line 40.... I'm completely new to unity as of a week ago, so I'll see if I can fix it. It literally just worked yesterday.
     
  7. sudogetpizza

    sudogetpizza

    Joined:
    Mar 13, 2019
    Posts:
    3
    I have a version here which gets all .unity files from a specified folder and adds them to the build settings:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.IO;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. public class Add_Scenes : EditorWindow
    7. {
    8.     [MenuItem ("MyTools/AddScenesToBuild")]
    9.  
    10.  
    11.  
    12.     static void Init ()
    13.     {
    14.         List<EditorBuildSettingsScene> editorBuildSettingsScenes = new List<EditorBuildSettingsScene>();
    15.         List<string> SceneList =  new List<string> ();
    16.         string MainFolder   = "Assets/Scenes";
    17.        
    18.  
    19.         DirectoryInfo d = new DirectoryInfo(@MainFolder);
    20.         FileInfo[] Files = d.GetFiles("*.unity"); //Getting unity files
    21.        
    22.         foreach(FileInfo file in Files )
    23.         {
    24.             Debug.Log ("file name:" + file.Name);
    25.           SceneList.Add(file.Name);
    26.         }
    27.        
    28.        
    29.      
    30.      
    31.         int i = 0;
    32.        
    33.      
    34.         for (i = 0; i < SceneList.Count; i ++)
    35.         {
    36.             string scenePath = MainFolder + "/" + SceneList[i];
    37.             Debug.Log ("i = " + i);
    38.             Debug.Log("scene path:" + scenePath);
    39.             editorBuildSettingsScenes.Add(new EditorBuildSettingsScene(scenePath, true));
    40.            
    41.          
    42.         }
    43.      
    44.         EditorBuildSettings.scenes = editorBuildSettingsScenes.ToArray();
    45.     }
    46.  
    47. }
     
    Chopium and mikaelK like this.
  8. mechane

    mechane

    Joined:
    Nov 17, 2018
    Posts:
    6
    I have a question : where do you put this file ? is it launched whenever we build the project ? thx in advance

    EDIT: I guess every script inheriting from UnityEditor is initialised before the build.
     
    Last edited: Dec 2, 2019
  9. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,866