Search Unity

[Unity 5] Assigning a Scene to Build Settings slot 0 Programmatically

Discussion in 'Editor & General Support' started by Studio_Akiba, Jul 30, 2020.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,422
    Is it possible inside Unity (5.6.7f1) to add a specific scene from a path to the build settings (slot 0) in c#?

    Bonus, I'm also trying to work out how to check if the scene is already in that position in the build settings, but I haven't been able to find anything online about assigning a specific scene to a specific slot in there and thought I'd ask you guys to see if anyone knows how to do any of this.
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Code (CSharp):
    1. BuildPlayerOptions options = new BuildPlayerOptions();
    2. options.scenes = new string[]
    3. {
    4.   "Assets/Scenes/Default.unity",
    5.   "Assets/Scenes/Another.unity"
    6. };
    7. BuildPipeline.BuildPlayer(options);
     
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,422
    Sorry, specifically want to add it to the top of the list regardless of what is already in there.
    Is it possible to grab the existing options and add to the top?
    That way I could check if it is already in there.
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    So you'll need to get the current build options for .scenes - a quick search came up with this:
    https://answers.unity.com/questions/1642506/getting-the-current-buildoptions.html
     
  5. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,422
    As I can't seem to use "BuildPlayerWindow" at all due to protection level, I decided to just have a manual button that overrides the existing stuff in the editor, but it isn't working.

    This is in the editor window.
    Code (CSharp):
    1. if(GUILayout.Button("Add '_preload' to Build Settings"))
    2.             {
    3.                 ResetBuildSettings();
    4.             }
    And this is its call, but for some reason it doesn't appear ot do anything or even trigger any errors.
    Code (CSharp):
    1. void ResetBuildSettings()
    2.     {
    3.         BuildPlayerOptions bpo = new BuildPlayerOptions();
    4.         bpo.scenes = new string[]
    5.         {
    6.             "Assets/ArcaneEngine/Scenes/_preload.unity"
    7.         };
    8.  
    9.         BuildPipeline.BuildPlayer(bpo);
    10.     }
     
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Try:
    Code (CSharp):
    1. BuildPlayerOptions options = new BuildPlayerOptions();
    2. options.scenes = new string[]
    3. {
    4.   "Assets/ArcaneEngine/Scenes/_preload.unity"
    5. }
    6.  
    7. options.locationPathName = path + "\\name.exe"; // Windows
    8. // options.locationPathName = path + "\\name.apk"; // Android
    9. // options.locationPathName = path + "\\name.x86_64"; // Linux
    10. // options.locationPathName = path; // WebGL
    11.  
    12. options.target = EditorUserBuildSettings.activeBuildTarget;
    13.  
    14. BuildPipeline.BuildPlayer(options);