Skip to content

Commit 35acae7

Browse files
committed
Improve build script for unity 2021.3
1 parent 532f9bc commit 35acae7

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

Assets/Scripts/Editor/BuildScript.cs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static class BuildScript
5757
{ "androidKeystorePass", "androidKeyaliasName", "androidKeyaliasPass" };
5858

5959
private static BuildPlayerOptions buildPlayerOptions;
60+
private static List<string> errorLogMessages = new List<string>();
6061

6162
[UsedImplicitly]
6263
public static void BuildWithCommandlineArgs()
@@ -121,26 +122,32 @@ public static void Build(string[] args)
121122
if (tagParameters.Contains("minsize"))
122123
{
123124
PlayerSettings.WebGL.template = "PROJECT:Release";
124-
SetWebGlOptimization(CodeOptimizationSize);
125125
buildPlayerOptions.options |= BuildOptions.CompressWithLz4HC;
126126
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.None;
127-
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
127+
SetWebGlOptimization(CodeOptimizationSize);
128128
#if UNITY_2022_1_OR_NEWER
129129
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
130+
#endif
131+
#if UNITY_2021_2_OR_NEWER
132+
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
133+
#else
134+
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
130135
#endif
131136
}
132137
else if (tagParameters.Contains("debug"))
133138
{
134139
PlayerSettings.WebGL.template = "PROJECT:Develop";
135140
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.FullWithStacktrace;
136-
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Debug);
141+
// For debug builds this setting will always be build times no matter what is set, setting this more as a documentation of the behavior
137142
SetWebGlOptimization(CodeOptimizationBuildTimes);
138143
#if UNITY_2022_1_OR_NEWER
139144
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
140145
#endif
141146
#if UNITY_2021_2_OR_NEWER
147+
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Debug);
142148
PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
143149
#else
150+
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Debug);
144151
PlayerSettings.WebGL.debugSymbols = true;
145152
#endif
146153

@@ -152,18 +159,27 @@ public static void Build(string[] args)
152159
else
153160
{
154161
PlayerSettings.WebGL.template = "PROJECT:Develop";
155-
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
156162
// By default use the speed setting
157163
SetWebGlOptimization(CodeOptimizationSpeed);
158164
#if UNITY_2022_1_OR_NEWER
159165
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSpeed);
166+
#endif
167+
#if UNITY_2021_2_OR_NEWER
168+
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
169+
#else
170+
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
160171
#endif
161172
}
162173

163174
List<GraphicsDeviceType> graphicsAPIs = new List<GraphicsDeviceType>();
164175
if (tagParameters.Contains("webgl1"))
165176
{
177+
#if !UNITY_2023_1_OR_NEWER
166178
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES2);
179+
#else
180+
LogWarning("WebGL1 not supported anymore, choosing WebGL2 instead");
181+
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
182+
#endif
167183
}
168184
if(tagParameters.Contains("webgl2"))
169185
{
@@ -173,19 +189,14 @@ public static void Build(string[] args)
173189
{
174190
#if UNITY_2023_2_OR_NEWER
175191
graphicsAPIs.Add(GraphicsDeviceType.WebGPU);
192+
// Enable wasm2023 for WebGPU, since if webGPU is supported everything from 2023 is supported as well
193+
PlayerSettings.WebGL.wasm2023 = true;
176194
#else
177195
LogError("WebGPU not supported yet");
178196
#endif
179197
}
180198

181199
PlayerSettings.SetGraphicsAPIs(BuildTarget.WebGL, graphicsAPIs.ToArray());
182-
183-
#if UNITY_2023_1_OR_NEWER
184-
if (tagParameters.Contains("webgl1"))
185-
{
186-
Log("WebGL1 not supported anymore, choosing WebGL2 instead");
187-
}
188-
#endif
189200
}
190201

191202
break;
@@ -203,11 +214,22 @@ public static void Build(string[] args)
203214
BackupLastBuild($"{projectPath}/{options["customBuildPath"]}");
204215
}
205216

217+
errorLogMessages = new List<string>();
218+
Application.logMessageReceived += OnLogMessageReceived;
219+
206220
// Custom build
207221
Build(buildTarget, options["customBuildPath"]);
208222
}
209223

210-
private static void BackupLastBuild(string buildPath)
224+
private static void OnLogMessageReceived(string logString, string stackTrace, LogType type)
225+
{
226+
if(type == LogType.Error || type == LogType.Exception)
227+
{
228+
errorLogMessages.Add($"{logString}{Eol}{stackTrace}");
229+
}
230+
}
231+
232+
private static void BackupLastBuild(string buildPath)
211233
{
212234
if (Directory.Exists(buildPath))
213235
{
@@ -342,6 +364,12 @@ private static void ReportSummary(BuildSummary summary)
342364
$"Size: {summary.totalSize.ToString()} bytes{Eol}" +
343365
$"{Eol}";
344366

367+
if(errorLogMessages.Count > 0)
368+
{
369+
summaryText += $"### Error log messages: ###{Eol}";
370+
summaryText += string.Join(Eol, errorLogMessages);
371+
}
372+
345373
if (summary.totalErrors == 0)
346374
{
347375
Log(summaryText);
@@ -421,6 +449,18 @@ private static void Log(string message)
421449
}
422450
}
423451

452+
private static void LogWarning(string message)
453+
{
454+
if(Application.isBatchMode)
455+
{
456+
Console.WriteLine(message);
457+
}
458+
else
459+
{
460+
Debug.LogWarning(message);
461+
}
462+
}
463+
424464
private static void LogError(string message)
425465
{
426466
if(Application.isBatchMode)

0 commit comments

Comments
 (0)