|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using UnityBuilderAction.Input; |
| 5 | +using UnityBuilderAction.Reporting; |
| 6 | +using UnityBuilderAction.Versioning; |
| 7 | +using UnityEditor; |
| 8 | +using UnityEditor.Build.Reporting; |
| 9 | +using UnityEngine; |
| 10 | + |
| 11 | +namespace UnityBuilderAction |
| 12 | +{ |
| 13 | + static class Builder |
| 14 | + { |
| 15 | + public static void BuildProject() |
| 16 | + { |
| 17 | + // Gather values from args |
| 18 | + var options = ArgumentsParser.GetValidatedOptions(); |
| 19 | + |
| 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 | + // Set version for this build |
| 52 | + 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 | + |
| 61 | + // Execute default AddressableAsset content build, if the package is installed. |
| 62 | + // Version defines would be the best solution here, but Unity 2018 doesn't support that, |
| 63 | + // so we fall back to using reflection instead. |
| 64 | + var addressableAssetSettingsType = Type.GetType( |
| 65 | + "UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,Unity.Addressables.Editor"); |
| 66 | + if (addressableAssetSettingsType != null) |
| 67 | + { |
| 68 | + // ReSharper disable once PossibleNullReferenceException, used from try-catch |
| 69 | + try |
| 70 | + { |
| 71 | + addressableAssetSettingsType.GetMethod("CleanPlayerContent", BindingFlags.Static | BindingFlags.Public) |
| 72 | + .Invoke(null, new object[] {null}); |
| 73 | + addressableAssetSettingsType.GetMethod("BuildPlayerContent", new Type[0]).Invoke(null, new object[0]); |
| 74 | + } |
| 75 | + catch (Exception e) |
| 76 | + { |
| 77 | + Debug.LogError($"Failed to run default addressables build:\n{e}"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Perform build |
| 82 | + BuildReport buildReport = BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 83 | + |
| 84 | + // Summary |
| 85 | + BuildSummary summary = buildReport.summary; |
| 86 | + StdOutReporter.ReportSummary(summary); |
| 87 | + |
| 88 | + // Result |
| 89 | + BuildResult result = summary.result; |
| 90 | + StdOutReporter.ExitWithResult(result); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments