1414using System . Linq ;
1515using JetBrains . Annotations ;
1616using UnityEditor ;
17+ using UnityEditor . Build ;
1718using UnityEditor . Build . Reporting ;
19+ using UnityEditor . Compilation ;
1820using UnityEngine ;
1921using UnityEngine . Rendering ;
2022
@@ -31,6 +33,25 @@ public static class BuildScript
3133 private static readonly string Eol = Environment . NewLine ;
3234 private static bool LogVerboseBatchMode = true ;
3335 private static bool LogVerboseInEditor = false ;
36+ private static readonly string CodeOptimizationSpeed =
37+ #if UNITY_2021_3_OR_NEWER
38+ CodeOptimizationWebGL . RuntimeSpeedLTO . ToString ( ) ;
39+ #else
40+ "speed" ;
41+ #endif
42+ private static readonly string CodeOptimizationSize =
43+ #if UNITY_2021_3_OR_NEWER
44+ CodeOptimizationWebGL . DiskSizeLTO . ToString ( ) ;
45+ #else
46+ "size" ;
47+ #endif
48+
49+ private static readonly string CodeOptimizationBuildTimes =
50+ #if UNITY_2021_3_OR_NEWER
51+ CodeOptimizationWebGL . BuildTimes . ToString ( ) ;
52+ #else
53+ "size" ;
54+ #endif
3455
3556 private static readonly string [ ] Secrets =
3657 { "androidKeystorePass" , "androidKeyaliasName" , "androidKeyaliasPass" } ;
@@ -51,10 +72,16 @@ public static void Build(string[] args)
5172 // Gather values from args
5273 Dictionary < string , string > options = GetValidatedOptions ( args ) ;
5374
54- // Set version for this build
55- PlayerSettings . bundleVersion = options [ "buildVersion" ] ;
56- PlayerSettings . macOS . buildNumber = options [ "buildVersion" ] ;
57- PlayerSettings . Android . bundleVersionCode = int . Parse ( options [ "androidVersionCode" ] ) ;
75+ // Set version for this build if provided
76+ if ( options . TryGetValue ( "buildVersion" , out string buildVersion ) && buildVersion != "none" )
77+ {
78+ PlayerSettings . bundleVersion = buildVersion ;
79+ PlayerSettings . macOS . buildNumber = buildVersion ;
80+ }
81+ if ( options . TryGetValue ( "androidVersionCode" , out string versionCode ) && versionCode != "0" )
82+ {
83+ PlayerSettings . Android . bundleVersionCode = int . Parse ( options [ "androidVersionCode" ] ) ;
84+ }
5885
5986 // Apply build target
6087 var buildTarget = ( BuildTarget ) Enum . Parse ( typeof ( BuildTarget ) , options [ "buildTarget" ] ) ;
@@ -84,6 +111,7 @@ public static void Build(string[] args)
84111#if UNITY_2021_2_OR_NEWER
85112 // Use ASTC texture compression, since we are also targeting mobile versions - Don't use this for desktop only targets
86113 buildPlayerOptions . subtarget = ( int ) WebGLTextureSubtarget . ASTC ;
114+ var namedBuildTarget = NamedBuildTarget . WebGL ;
87115#endif
88116
89117 if ( options . TryGetValue ( "tag" , out string tagVersion ) &&
@@ -93,16 +121,23 @@ public static void Build(string[] args)
93121 if ( tagParameters . Contains ( "minsize" ) )
94122 {
95123 PlayerSettings . WebGL . template = "PROJECT:Release" ;
96- SetWebGlOptimization ( "size" ) ;
124+ SetWebGlOptimization ( CodeOptimizationSize ) ;
125+ buildPlayerOptions . options |= BuildOptions . CompressWithLz4HC ;
97126 PlayerSettings . WebGL . exceptionSupport = WebGLExceptionSupport . None ;
98127 PlayerSettings . SetIl2CppCompilerConfiguration ( BuildTargetGroup . WebGL , Il2CppCompilerConfiguration . Master ) ;
128+ #if UNITY_2022_1_OR_NEWER
129+ PlayerSettings . SetIl2CppCodeGeneration ( namedBuildTarget , Il2CppCodeGeneration . OptimizeSize ) ;
130+ #endif
99131 }
100132 else if ( tagParameters . Contains ( "debug" ) )
101133 {
102134 PlayerSettings . WebGL . template = "PROJECT:Develop" ;
103- SetWebGlOptimization ( "size" ) ;
104135 PlayerSettings . WebGL . exceptionSupport = WebGLExceptionSupport . FullWithStacktrace ;
105136 PlayerSettings . SetIl2CppCompilerConfiguration ( BuildTargetGroup . WebGL , Il2CppCompilerConfiguration . Debug ) ;
137+ SetWebGlOptimization ( CodeOptimizationBuildTimes ) ;
138+ #if UNITY_2022_1_OR_NEWER
139+ PlayerSettings . SetIl2CppCodeGeneration ( namedBuildTarget , Il2CppCodeGeneration . OptimizeSize ) ;
140+ #endif
106141#if UNITY_2021_2_OR_NEWER
107142 PlayerSettings . WebGL . debugSymbolMode = WebGLDebugSymbolMode . Embedded ;
108143#else
@@ -117,8 +152,12 @@ public static void Build(string[] args)
117152 else
118153 {
119154 PlayerSettings . WebGL . template = "PROJECT:Develop" ;
155+ PlayerSettings . SetIl2CppCompilerConfiguration ( BuildTargetGroup . WebGL , Il2CppCompilerConfiguration . Master ) ;
120156 // By default use the speed setting
121- SetWebGlOptimization ( "speed" ) ;
157+ SetWebGlOptimization ( CodeOptimizationSpeed ) ;
158+ #if UNITY_2022_1_OR_NEWER
159+ PlayerSettings . SetIl2CppCodeGeneration ( namedBuildTarget , Il2CppCodeGeneration . OptimizeSpeed ) ;
160+ #endif
122161 }
123162
124163 List < GraphicsDeviceType > graphicsAPIs = new List < GraphicsDeviceType > ( ) ;
@@ -155,7 +194,7 @@ public static void Build(string[] args)
155194 // Additional options for local builds
156195 if ( ! Application . isBatchMode )
157196 {
158- if ( options . TryGetValue ( "autorunplayer" , out string autorunplayer ) )
197+ if ( options . TryGetValue ( "autorunplayer" , out string _ ) )
159198 {
160199 buildPlayerOptions . options |= BuildOptions . AutoRunPlayer ;
161200 }
0 commit comments