Getting the current BuildOptions

I’m trying to autmate the build process and I have the following problem:

When calling BuildPipeline.BuildPlayer it expects a BuildPlayerOptions object with a BuildOptions field. I would like to fill this field with the currently selected items in the BuildSettings window.

Previousely EditorUserBuildSettings.architectureFlags was used for this, however this is now no longer part of Unity.

The BuildSettings window settings also don’t seem to be saved in the EditorBuildSettings.asset file in the ProjectSettings window, so parsing the YAML file is also out of the question.

Essentially I would like to do something like this:

BuildPlayerOptions buildOptions = GetCurrentBuildOptions();

But no such thing seems to exist as far as I can see by reading the documentation.

Thanks in advance!

BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions() (source) might be what you’re looking for. It calls the internal function GetBuildPlayerOptionsInternal() and will always open a window prompting for a output directory. GetBuildPlayerOptions() takes a “default” instance of BuildPlayerOptions as a parameter, but you can pass a new instance if you don’t have any specific options you’d like to provide as default.

If you don’t mind being prompted for a build folder, your GetCurrentBuildOptions() method could look something like this:

static BuildPlayerOptions GetCurrentBuildOptions(
    BuildPlayerOptions defaultOptions = new BuildPlayerOptions()) 
    return BuildPlayerWindow.DefaultBuildMethods.GetBuldPlayerOptions(defaultOptions);

If you don’t want to be prompted for an output folder, things get a little trickier. In this case, I’d use reflection:

static BuildPlayerOptions GetBuildPlayerOptions(
	bool askForLocation = false,
	BuildPlayerOptions defaultOptions = new BuildPlayerOptions())
	// Get static internal "GetBuildPlayerOptionsInternal" method
	MethodInfo method = typeof(BuildPlayerWindow).GetMethod(
		BindingFlags.NonPublic | BindingFlags.Static);

	// invoke internal method
	return (BuildPlayerOptions)method.Invoke(
		new object[] { askForLocation, defaultOptions});

As always with reflection, be aware that the internal method signature is subject to change, and may cause your build scripts to break in future editor versions.

@Minchuilla’s addition is still working as of 2019.4.4f1.

It kills me that turning off Development Build doesn’t turn off the #DEVELOPMENT_BUILD define. What a weird thing not to do…? I’m trying to have a dynamic build path for Addressable content bundles and this has been one of the hardest parts!