-
Notifications
You must be signed in to change notification settings - Fork 54
/
BuildScript.cs
476 lines (431 loc) · 14.5 KB
/
BuildScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BuildScript.cs">
// Copyright (c) 2023 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// </author>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Compilation;
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityBuilderAction
{
/// <summary>
/// Used for building the project through continuous integration (CI) or semi-automated through menu items
/// Supports logging in the editor and highly configurable WebGL
/// Modified version of <see href="https://github.com/game-ci/documentation/blob/main/example/BuildScript.cs" />
/// Tailored to the needs for <see href="https://github.com/JohannesDeml/UnityWebGL-LoadingTest" />
/// </summary>
public static class BuildScript
{
private static readonly string Eol = Environment.NewLine;
private static bool LogVerboseBatchMode = true;
private static bool LogVerboseInEditor = false;
private static readonly string CodeOptimizationSpeed =
#if UNITY_2021_3_OR_NEWER
CodeOptimizationWebGL.RuntimeSpeedLTO.ToString();
#else
"speed";
#endif
private static readonly string CodeOptimizationSize =
#if UNITY_2021_3_OR_NEWER
CodeOptimizationWebGL.DiskSizeLTO.ToString();
#else
"size";
#endif
private static readonly string CodeOptimizationBuildTimes =
#if UNITY_2021_3_OR_NEWER
CodeOptimizationWebGL.BuildTimes.ToString();
#else
"size";
#endif
private static readonly string[] Secrets =
{ "androidKeystorePass", "androidKeyaliasName", "androidKeyaliasPass" };
private static BuildPlayerOptions buildPlayerOptions;
private static List<string> errorLogMessages = new List<string>();
[UsedImplicitly]
public static void BuildWithCommandlineArgs()
{
string[] args = Environment.GetCommandLineArgs();
Build(args);
}
public static void Build(string[] args)
{
buildPlayerOptions = new BuildPlayerOptions();
// Gather values from args
Dictionary<string, string> options = GetValidatedOptions(args);
// Set version for this build if provided
if(options.TryGetValue("buildVersion", out string buildVersion) && buildVersion != "none")
{
PlayerSettings.bundleVersion = buildVersion;
PlayerSettings.macOS.buildNumber = buildVersion;
}
if(options.TryGetValue("androidVersionCode", out string versionCode) && versionCode != "0")
{
PlayerSettings.Android.bundleVersionCode = int.Parse(options["androidVersionCode"]);
}
// Apply build target
var buildTarget = (BuildTarget)Enum.Parse(typeof(BuildTarget), options["buildTarget"]);
switch (buildTarget)
{
case BuildTarget.Android:
{
EditorUserBuildSettings.buildAppBundle = options["customBuildPath"].EndsWith(".aab");
if (options.TryGetValue("androidKeystoreName", out string keystoreName) &&
!string.IsNullOrEmpty(keystoreName))
PlayerSettings.Android.keystoreName = keystoreName;
if (options.TryGetValue("androidKeystorePass", out string keystorePass) &&
!string.IsNullOrEmpty(keystorePass))
PlayerSettings.Android.keystorePass = keystorePass;
if (options.TryGetValue("androidKeyaliasName", out string keyaliasName) &&
!string.IsNullOrEmpty(keyaliasName))
PlayerSettings.Android.keyaliasName = keyaliasName;
if (options.TryGetValue("androidKeyaliasPass", out string keyaliasPass) &&
!string.IsNullOrEmpty(keyaliasPass))
PlayerSettings.Android.keyaliasPass = keyaliasPass;
break;
}
case BuildTarget.StandaloneOSX:
PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.Mono2x);
break;
case BuildTarget.WebGL:
#if UNITY_2021_2_OR_NEWER
// Use ASTC texture compression, since we are also targeting mobile versions - Don't use this for desktop only targets
buildPlayerOptions.subtarget = (int)WebGLTextureSubtarget.ASTC;
var namedBuildTarget = NamedBuildTarget.WebGL;
#endif
if (options.TryGetValue("tag", out string tagVersion) &&
!string.IsNullOrEmpty(tagVersion))
{
string[] tagParameters = tagVersion.Split('-');
if (tagParameters.Contains("minsize"))
{
PlayerSettings.WebGL.template = "PROJECT:Release";
buildPlayerOptions.options |= BuildOptions.CompressWithLz4HC;
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.None;
SetWebGlOptimization(CodeOptimizationSize);
#if UNITY_2022_1_OR_NEWER
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
#endif
#if UNITY_2021_2_OR_NEWER
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
#else
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
#endif
}
else if (tagParameters.Contains("debug"))
{
PlayerSettings.WebGL.template = "PROJECT:Develop";
PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.FullWithStacktrace;
// For debug builds this setting will always be build times no matter what is set, setting this more as a documentation of the behavior
SetWebGlOptimization(CodeOptimizationBuildTimes);
#if UNITY_2022_1_OR_NEWER
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSize);
#endif
#if UNITY_2021_2_OR_NEWER
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Debug);
PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
#else
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Debug);
PlayerSettings.WebGL.debugSymbols = true;
#endif
#if UNITY_2022_2_OR_NEWER
PlayerSettings.WebGL.showDiagnostics = true;
#endif
buildPlayerOptions.options |= BuildOptions.Development;
}
else
{
PlayerSettings.WebGL.template = "PROJECT:Develop";
// By default use the speed setting
SetWebGlOptimization(CodeOptimizationSpeed);
#if UNITY_2022_1_OR_NEWER
PlayerSettings.SetIl2CppCodeGeneration(namedBuildTarget, Il2CppCodeGeneration.OptimizeSpeed);
#endif
#if UNITY_2021_2_OR_NEWER
PlayerSettings.SetIl2CppCompilerConfiguration(namedBuildTarget, Il2CppCompilerConfiguration.Master);
#else
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Master);
#endif
}
List<GraphicsDeviceType> graphicsAPIs = new List<GraphicsDeviceType>();
if (tagParameters.Contains("webgl1"))
{
#if !UNITY_2023_1_OR_NEWER
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES2);
#else
LogWarning("WebGL1 not supported anymore, choosing WebGL2 instead");
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
#endif
}
if(tagParameters.Contains("webgl2"))
{
graphicsAPIs.Add(GraphicsDeviceType.OpenGLES3);
}
if(tagParameters.Contains("webgpu"))
{
#if UNITY_2023_2_OR_NEWER
graphicsAPIs.Add(GraphicsDeviceType.WebGPU);
// Enable wasm2023 for WebGPU, since if webGPU is supported everything from 2023 is supported as well
PlayerSettings.WebGL.wasm2023 = true;
#else
LogError("WebGPU not supported yet");
#endif
}
PlayerSettings.SetGraphicsAPIs(BuildTarget.WebGL, graphicsAPIs.ToArray());
}
break;
}
// Additional options for local builds
if (!Application.isBatchMode)
{
if (options.TryGetValue("autorunplayer", out string _))
{
buildPlayerOptions.options |= BuildOptions.AutoRunPlayer;
}
var projectPath = Application.dataPath.Substring(0, Application.dataPath.Length - "/Assets".Length);
BackupLastBuild($"{projectPath}/{options["customBuildPath"]}");
}
errorLogMessages = new List<string>();
Application.logMessageReceived += OnLogMessageReceived;
// Custom build
Build(buildTarget, options["customBuildPath"]);
}
private static void OnLogMessageReceived(string logString, string stackTrace, LogType type)
{
if(type == LogType.Error || type == LogType.Exception)
{
errorLogMessages.Add($"{logString}{Eol}{stackTrace}");
}
}
private static void BackupLastBuild(string buildPath)
{
if (Directory.Exists(buildPath))
{
string backupFolderPath = $"{buildPath}-Previous";
if (Directory.Exists(backupFolderPath))
{
Directory.Delete(backupFolderPath, true);
}
Log($"Moving current build folder to backup location: {backupFolderPath}");
Directory.Move(buildPath, backupFolderPath);
}
else if (File.Exists(buildPath))
{
string extension = Path.GetExtension(buildPath);
string pathWithoutExtension = buildPath.Substring(0, buildPath.Length - extension.Length);
string backupFilePath = $"{pathWithoutExtension}-Previous{extension}";
if (File.Exists(backupFilePath))
{
File.Delete(backupFilePath);
}
Log($"Moving current build file to backup location: {backupFilePath}");
File.Move(buildPath, backupFilePath);
}
}
private static void SetWebGlOptimization(string value)
{
#if UNITY_2019_4_OR_NEWER
EditorUserBuildSettings.SetPlatformSettings(BuildPipeline.GetBuildTargetName(BuildTarget.WebGL),
"CodeOptimization", value);
#else
Log($"Setting {nameof(SetWebGlOptimization)} not supported by this unity version");
#endif
}
private static Dictionary<string, string> GetValidatedOptions(string[] args)
{
ParseCommandLineArguments(args, out Dictionary<string, string> validatedOptions);
if (!validatedOptions.TryGetValue("projectPath", out string _))
{
LogError("Missing argument -projectPath");
EndBuild(110);
}
if (!validatedOptions.TryGetValue("buildTarget", out string buildTarget))
{
LogError("Missing argument -buildTarget");
EndBuild(120);
}
if (!Enum.IsDefined(typeof(BuildTarget), buildTarget ?? string.Empty))
{
EndBuild(121);
}
if (!validatedOptions.TryGetValue("customBuildPath", out string _))
{
LogError("Missing argument -customBuildPath");
EndBuild(130);
}
const string defaultCustomBuildName = "TestBuild";
if (!validatedOptions.TryGetValue("customBuildName", out string customBuildName))
{
LogError($"Missing argument -customBuildName, defaulting to {defaultCustomBuildName}.");
validatedOptions.Add("customBuildName", defaultCustomBuildName);
}
else if (customBuildName == "")
{
LogError($"Invalid argument -customBuildName, defaulting to {defaultCustomBuildName}.");
validatedOptions.Add("customBuildName", defaultCustomBuildName);
}
return validatedOptions;
}
private static void ParseCommandLineArguments(string[] args, out Dictionary<string, string> providedArguments)
{
providedArguments = new Dictionary<string, string>();
LogVerbose(
$"{Eol}" +
$"###########################{Eol}" +
$"# Parsing settings #{Eol}" +
$"###########################{Eol}" +
$"{Eol}"
);
// Extract flags with optional values
for (int current = 0, next = 1; current < args.Length; current++, next++)
{
// Parse flag
bool isFlag = args[current].StartsWith("-");
if (!isFlag) continue;
string flag = args[current].TrimStart('-');
// Parse optional value
bool flagHasValue = next < args.Length && !args[next].StartsWith("-");
string value = flagHasValue ? args[next].TrimStart('-') : "";
bool secret = Secrets.Contains(flag);
string displayValue = secret ? "*HIDDEN*" : "\"" + value + "\"";
// Assign
LogVerbose($"Found flag \"{flag}\" with value {displayValue}.");
providedArguments.Add(flag, value);
}
}
private static void Build(BuildTarget buildTarget, string filePath)
{
string[] scenes = EditorBuildSettings.scenes.Where(scene => scene.enabled).Select(s => s.path).ToArray();
buildPlayerOptions.scenes = scenes;
buildPlayerOptions.target = buildTarget;
buildPlayerOptions.locationPathName = filePath;
BuildSummary buildSummary = BuildPipeline.BuildPlayer(buildPlayerOptions).summary;
ReportSummary(buildSummary);
ExitWithResult(buildSummary.result);
}
private static void ReportSummary(BuildSummary summary)
{
string summaryText = $"{Eol}" +
$"###########################{Eol}" +
$"# Build results #{Eol}" +
$"###########################{Eol}" +
$"{Eol}" +
$"Duration: {summary.totalTime.ToString()}{Eol}" +
$"Warnings: {summary.totalWarnings.ToString()}{Eol}" +
$"Errors: {summary.totalErrors.ToString()}{Eol}" +
$"Size: {summary.totalSize.ToString()} bytes{Eol}" +
$"{Eol}";
if(errorLogMessages.Count > 0)
{
summaryText += $"### Error log messages: ###{Eol}";
summaryText += string.Join(Eol, errorLogMessages);
}
if (summary.totalErrors == 0)
{
Log(summaryText);
}
else
{
LogError(summaryText);
}
}
private static void ExitWithResult(BuildResult result)
{
switch (result)
{
case BuildResult.Succeeded:
Log("Build succeeded!");
EndBuild(0);
break;
case BuildResult.Failed:
LogError("Build failed!");
EndBuild(101);
break;
case BuildResult.Cancelled:
LogError("Build cancelled!");
EndBuild(102);
break;
case BuildResult.Unknown:
default:
LogError("Build result is unknown!");
EndBuild(103);
break;
}
}
private static void EndBuild(int returnValue)
{
if (Application.isBatchMode)
{
EditorApplication.Exit(returnValue);
}
else
{
if (returnValue != 0)
{
throw new Exception($"BuildScript ended with non-zero exitCode: {returnValue}");
}
}
}
private static void LogVerbose(string message)
{
if(Application.isBatchMode)
{
if (LogVerboseBatchMode)
{
Console.WriteLine(message);
}
}
else
{
if (LogVerboseInEditor)
{
Debug.Log(message);
}
}
}
private static void Log(string message)
{
if(Application.isBatchMode)
{
Console.WriteLine(message);
}
else
{
Debug.Log(message);
}
}
private static void LogWarning(string message)
{
if(Application.isBatchMode)
{
Console.WriteLine(message);
}
else
{
Debug.LogWarning(message);
}
}
private static void LogError(string message)
{
if(Application.isBatchMode)
{
Console.WriteLine(message);
}
else
{
Debug.LogError(message);
}
}
}
}