66using UnityBuilderAction . Versioning ;
77using UnityEditor ;
88using UnityEditor . Build . Reporting ;
9+ #if UNITY_6000_0_OR_NEWER
10+ using UnityEditor . Build . Profile ;
11+ #endif
912using UnityEngine ;
1013
1114namespace UnityBuilderAction
@@ -17,47 +20,9 @@ public static void BuildProject()
1720 // Gather values from args
1821 var options = ArgumentsParser . GetValidatedOptions ( ) ;
1922
20- // Gather values from project
21- var scenes = EditorBuildSettings . scenes . Where ( scene => scene . enabled ) . Select ( s => s . path ) . ToArray ( ) ;
22-
23- // Get all buildOptions from options
24- BuildOptions buildOptions = BuildOptions . None ;
25- foreach ( string buildOptionString in Enum . GetNames ( typeof ( BuildOptions ) ) ) {
26- if ( options . ContainsKey ( buildOptionString ) ) {
27- BuildOptions buildOptionEnum = ( BuildOptions ) Enum . Parse ( typeof ( BuildOptions ) , buildOptionString ) ;
28- buildOptions |= buildOptionEnum ;
29- }
30- }
31-
32- #if UNITY_2021_2_OR_NEWER
33- // Determine subtarget
34- StandaloneBuildSubtarget buildSubtarget ;
35- if ( ! options . TryGetValue ( "standaloneBuildSubtarget" , out var subtargetValue ) || ! Enum . TryParse ( subtargetValue , out buildSubtarget ) ) {
36- buildSubtarget = default ;
37- }
38- #endif
39-
40- // Define BuildPlayer Options
41- var buildPlayerOptions = new BuildPlayerOptions {
42- scenes = scenes ,
43- locationPathName = options [ "customBuildPath" ] ,
44- target = ( BuildTarget ) Enum . Parse ( typeof ( BuildTarget ) , options [ "buildTarget" ] ) ,
45- options = buildOptions ,
46- #if UNITY_2021_2_OR_NEWER
47- subtarget = ( int ) buildSubtarget
48- #endif
49- } ;
50-
5123 // Set version for this build
5224 VersionApplicator . SetVersion ( options [ "buildVersion" ] ) ;
53-
54- // Apply Android settings
55- if ( buildPlayerOptions . target == BuildTarget . Android )
56- {
57- VersionApplicator . SetAndroidVersionCode ( options [ "androidVersionCode" ] ) ;
58- AndroidSettings . Apply ( options ) ;
59- }
60-
25+
6126 // Execute default AddressableAsset content build, if the package is installed.
6227 // Version defines would be the best solution here, but Unity 2018 doesn't support that,
6328 // so we fall back to using reflection instead.
@@ -78,6 +43,70 @@ public static void BuildProject()
7843 }
7944 }
8045
46+ // Get all buildOptions from options
47+ BuildOptions buildOptions = BuildOptions . None ;
48+ foreach ( string buildOptionString in Enum . GetNames ( typeof ( BuildOptions ) ) ) {
49+ if ( options . ContainsKey ( buildOptionString ) ) {
50+ BuildOptions buildOptionEnum = ( BuildOptions ) Enum . Parse ( typeof ( BuildOptions ) , buildOptionString ) ;
51+ buildOptions |= buildOptionEnum ;
52+ }
53+ }
54+
55+ // Depending on whether the build is using a build profile, `buildPlayerOptions` will an instance
56+ // of either `UnityEditor.BuildPlayerOptions` or `UnityEditor.BuildPlayerWithProfileOptions`
57+ dynamic buildPlayerOptions ;
58+
59+ if ( options . ContainsKey ( "buildProfile" ) ) {
60+
61+ #if UNITY_6000_0_OR_NEWER
62+ // Load build profile from Assets folder
63+ BuildProfile buildProfile = AssetDatabase . LoadAssetAtPath < BuildProfile > ( options [ "buildProfile" ] ) ;
64+
65+ // Set it as active
66+ BuildProfile . SetActiveBuildProfile ( buildProfile ) ;
67+
68+ // Define BuildPlayerWithProfileOptions
69+ buildPlayerOptions = new BuildPlayerWithProfileOptions {
70+ buildProfile = buildProfile ,
71+ locationPathName = options [ "customBuildPath" ] ,
72+ options = buildOptions ,
73+ } ;
74+ #endif
75+
76+ } else {
77+
78+ // Gather values from project
79+ var scenes = EditorBuildSettings . scenes . Where ( scene => scene . enabled ) . Select ( s => s . path ) . ToArray ( ) ;
80+
81+ #if UNITY_2021_2_OR_NEWER
82+ // Determine subtarget
83+ StandaloneBuildSubtarget buildSubtarget ;
84+ if ( ! options . TryGetValue ( "standaloneBuildSubtarget" , out var subtargetValue ) || ! Enum . TryParse ( subtargetValue , out buildSubtarget ) ) {
85+ buildSubtarget = default ;
86+ }
87+ #endif
88+
89+ BuildTarget buildTarget = ( BuildTarget ) Enum . Parse ( typeof ( BuildTarget ) , options [ "buildTarget" ] ) ;
90+
91+ // Define BuildPlayerOptions
92+ buildPlayerOptions = new BuildPlayerOptions {
93+ scenes = scenes ,
94+ locationPathName = options [ "customBuildPath" ] ,
95+ target = buildTarget ,
96+ options = buildOptions ,
97+ #if UNITY_2021_2_OR_NEWER
98+ subtarget = ( int ) buildSubtarget
99+ #endif
100+ } ;
101+
102+ // Apply Android settings
103+ if ( buildTarget == BuildTarget . Android ) {
104+ VersionApplicator . SetAndroidVersionCode ( options [ "androidVersionCode" ] ) ;
105+ AndroidSettings . Apply ( options ) ;
106+ }
107+
108+ }
109+
81110 // Perform build
82111 BuildReport buildReport = BuildPipeline . BuildPlayer ( buildPlayerOptions ) ;
83112
0 commit comments