- Home /
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!
Answer by nratcliff · Jun 25, 2019 at 12:55 PM
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(
"GetBuildPlayerOptionsInternal",
BindingFlags.NonPublic | BindingFlags.Static);
// invoke internal method
return (BuildPlayerOptions)method.Invoke(
null,
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.
Hey @nratcliff thanks for taking the time to answer. Reflection seems to be the (only?) way to get these settings. Since I don't have the luxury to ask for a directory due to the editor being run in headless mode
The function throws an exception for me due to EditorUserBuildSettings.GetBuildLocation(buildTarget);
not returning a valid path. So it seems I have to implement the GetDefaultBuildPlayerOptions
function myself based on the documentation on github. Which is unfortunate.
I'll mark your answer as accepted for now, since there are no other/better answers at this time.
After changing the typeof(BuildPlayerWindow)
to typeof(BuildPlayerWindow.DefaultBuild$$anonymous$$ethods)
this worked for me in 2019.3.0f6
@$$anonymous$$inchuilla's addition is still working as of 2019.4.4f1.
It kills me that turning off Development Build doesn't turn off the #DEVELOP$$anonymous$$ENT_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!
In 2019.2.6 in the line:
MethodInfo method = typeof(BuildPlayerWindow).GetMethod(
"GetBuildPlayerOptionsInternal",
BindingFlags.NonPublic | BindingFlags.Static);
instead of that line you need to insert:
MethodInfo method = typeof(BuildPlayerWindow.DefaultBuildMethods).GetMethod(
"GetBuildPlayerOptionsInternal",
BindingFlags.NonPublic | BindingFlags.Static);
Your answer
Follow this Question
Related Questions
Set dynamicBatching player setting via editor script? 0 Answers
Is there a some sort of OnBuild() event for the editor? 1 Answer
What is the difference between BuildTarget and BuildTargetGroup 0 Answers
Share same assets with multiple standalone builds 0 Answers
Platform switch to trigger a function 0 Answers