Search Unity

Question Custom Build Settings window

Discussion in 'Editor & General Support' started by DevDunk, Oct 19, 2021.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,059
    I want to make a custom build settings window to add 1 or 2 extra checkboxes with their own functionality.
    Is there a way of doing this without making everything from scratch?
    I already have some experience with custom editors.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,059
  3. ExpiditionVortex

    ExpiditionVortex

    Joined:
    Nov 27, 2017
    Posts:
    4
    Any update?
     
  4. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    89
    Here's a base script for a custom secondary build window that opens after pressing Build or Build And Run in Unity. The Build button in this custom window will prompt for build location and do the build as normal

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class CustomBuildWindow : EditorWindow
    5. {
    6.     [InitializeOnLoadMethod]
    7.     private static void InitOnLoad()
    8.     {
    9.         // hijack the Build button in Unity's Build Settings window
    10.         BuildPlayerWindow.RegisterGetBuildPlayerOptionsHandler(OnGetBuildPlayerOptions);
    11.         BuildPlayerWindow.RegisterBuildPlayerHandler(OnBuildPlayer);
    12.     }
    13.  
    14.     private static BuildPlayerOptions OnGetBuildPlayerOptions(BuildPlayerOptions buildPlayerOptions)
    15.     {
    16.         OpenCustomBuildWindow(buildPlayerOptions);
    17.         return buildPlayerOptions;
    18.     }
    19.  
    20.     private static void OnBuildPlayer(BuildPlayerOptions buildPlayerOptions)
    21.     {
    22.         // Do nothing here as the build is triggered from the Build button of the custom window
    23.     }
    24.  
    25.     public static CustomBuildWindow OpenCustomBuildWindow(BuildPlayerOptions buildPlayerOptions)
    26.     {
    27.         var w = EditorWindow.GetWindow<CustomBuildWindow>();
    28.         // here you can store initial options from Unity's Build window, such as if "Build" or "Build And Run" was pressed
    29.         w.buildOptions = buildPlayerOptions.options;
    30.         w.Show();
    31.         return w;
    32.     }
    33.  
    34.     private BuildOptions buildOptions;
    35.  
    36.     private void OnEnable()
    37.     {
    38.         titleContent = new GUIContent("Custom Build Settings");
    39.     }
    40.  
    41.     private void OnGUI()
    42.     {
    43.         // add your GUI controls to modify the build here
    44.  
    45.         // Build button
    46.         string buildButtonLabel = (buildOptions & BuildOptions.AutoRunPlayer) == 0 ? "Build" : "Build And Run";
    47.         if (GUILayout.Button(buildButtonLabel))
    48.         {
    49.             // Remove this if you don't want to close the window when starting a build
    50.             Close();
    51.  
    52.             DoBuild();
    53.         }
    54.     }
    55.  
    56.     private void DoBuild()
    57.     {
    58.         var buildPlayerOptions = new BuildPlayerOptions();
    59.         buildPlayerOptions.options = buildOptions;
    60.         try
    61.         {
    62.             // This gets the default scene list and options from Unity's Build Settings window
    63.             // This prompts for the build output location and stores it in buildPlayerOptions.locationPathName
    64.             buildPlayerOptions = BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(buildPlayerOptions);
    65.         }
    66.         catch (BuildPlayerWindow.BuildMethodException)
    67.         {
    68.             // Hide an exception from log if user cancels the build location prompt
    69.             return;
    70.         }
    71.  
    72.         // Here you can modify the buildPlayerOptions or the project with values set in the window
    73.  
    74.         // Execute the build (using the default build method in this example)
    75.         BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(buildPlayerOptions);
    76.     }
    77. }