6
6
using UnityBuilderAction . Versioning ;
7
7
using UnityEditor ;
8
8
using UnityEditor . Build . Reporting ;
9
+ #if UNITY_6000_0_OR_NEWER
10
+ using UnityEditor . Build . Profile ;
11
+ #endif
9
12
using UnityEngine ;
10
13
11
14
namespace UnityBuilderAction
@@ -17,47 +20,9 @@ public static void BuildProject()
17
20
// Gather values from args
18
21
var options = ArgumentsParser . GetValidatedOptions ( ) ;
19
22
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
-
51
23
// Set version for this build
52
24
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
+
61
26
// Execute default AddressableAsset content build, if the package is installed.
62
27
// Version defines would be the best solution here, but Unity 2018 doesn't support that,
63
28
// so we fall back to using reflection instead.
@@ -78,6 +43,72 @@ public static void BuildProject()
78
43
}
79
44
}
80
45
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 [ "customBuildProfile" ] != "" ) {
60
+
61
+ #if UNITY_6000_0_OR_NEWER
62
+ // Load build profile from Assets folder
63
+ BuildProfile buildProfile = AssetDatabase . LoadAssetAtPath < BuildProfile > ( options [ "customBuildProfile" ] ) ;
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
+ #else
75
+ throw new Exception ( "Build profiles are not supported by this version of Unity (" + Application . unityVersion + ")" ) ;
76
+ #endif
77
+
78
+ } else {
79
+
80
+ // Gather values from project
81
+ var scenes = EditorBuildSettings . scenes . Where ( scene => scene . enabled ) . Select ( s => s . path ) . ToArray ( ) ;
82
+
83
+ #if UNITY_2021_2_OR_NEWER
84
+ // Determine subtarget
85
+ StandaloneBuildSubtarget buildSubtarget ;
86
+ if ( ! options . TryGetValue ( "standaloneBuildSubtarget" , out var subtargetValue ) || ! Enum . TryParse ( subtargetValue , out buildSubtarget ) ) {
87
+ buildSubtarget = default ;
88
+ }
89
+ #endif
90
+
91
+ BuildTarget buildTarget = ( BuildTarget ) Enum . Parse ( typeof ( BuildTarget ) , options [ "buildTarget" ] ) ;
92
+
93
+ // Define BuildPlayerOptions
94
+ buildPlayerOptions = new BuildPlayerOptions {
95
+ scenes = scenes ,
96
+ locationPathName = options [ "customBuildPath" ] ,
97
+ target = buildTarget ,
98
+ options = buildOptions ,
99
+ #if UNITY_2021_2_OR_NEWER
100
+ subtarget = ( int ) buildSubtarget
101
+ #endif
102
+ } ;
103
+
104
+ // Apply Android settings
105
+ if ( buildTarget == BuildTarget . Android ) {
106
+ VersionApplicator . SetAndroidVersionCode ( options [ "androidVersionCode" ] ) ;
107
+ AndroidSettings . Apply ( options ) ;
108
+ }
109
+
110
+ }
111
+
81
112
// Perform build
82
113
BuildReport buildReport = BuildPipeline . BuildPlayer ( buildPlayerOptions ) ;
83
114
0 commit comments