|
| 1 | +using LinkerGenerator; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.Build; |
| 6 | +using UnityEditor.Build.Reporting; |
| 7 | +using UnityEngine; |
| 8 | + |
| 9 | +public class BasisBuildDialogAndSettings : IPreprocessBuildWithReport |
| 10 | +{ |
| 11 | + public int callbackOrder => 0; |
| 12 | + |
| 13 | + // ====== Version bump config ====== |
| 14 | + private const bool AutoIncrementBundleVersion = true; // PlayerSettings.bundleVersion (also Android versionName) |
| 15 | + private const bool AutoIncrementAndroidVersionCode = true; // PlayerSettings.Android.bundleVersionCode |
| 16 | + |
| 17 | + // If true, forces bundleVersion into X.Y.Z format (best practice). |
| 18 | + private const bool ForceSemanticVersionFormat = true; |
| 19 | + |
| 20 | + // Platforms that effectively require IL2CPP (commonly true in modern Unity). |
| 21 | + private static readonly HashSet<BuildTarget> Il2CppOnlyTargets = new HashSet<BuildTarget> |
| 22 | + { |
| 23 | + BuildTarget.Android, |
| 24 | + BuildTarget.iOS, |
| 25 | + BuildTarget.tvOS, |
| 26 | + BuildTarget.WebGL, |
| 27 | +#if UNITY_2019_1_OR_NEWER |
| 28 | + BuildTarget.PS4, |
| 29 | + BuildTarget.XboxOne, |
| 30 | + BuildTarget.Switch, |
| 31 | +#endif |
| 32 | + }; |
| 33 | + |
| 34 | + // Platforms you want to force Mono (example: your Linux choice). |
| 35 | + private static readonly HashSet<BuildTarget> MonoOnlyTargets = new HashSet<BuildTarget> |
| 36 | + { |
| 37 | + BuildTarget.StandaloneLinux64, |
| 38 | + BuildTarget.LinuxHeadlessSimulation, |
| 39 | + }; |
| 40 | + |
| 41 | + public void OnPreprocessBuild(BuildReport report) |
| 42 | + { |
| 43 | + // 0) Generate link.xml |
| 44 | + BasisLinkGenerator.GenerateLinkXml(); |
| 45 | + |
| 46 | + // 0.5) Versioning |
| 47 | + BumpVersionsIfNeeded(report.summary.platform); |
| 48 | + |
| 49 | + var namedBuildTarget = |
| 50 | + UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(report.summary.platformGroup); |
| 51 | + |
| 52 | + var currentBackend = PlayerSettings.GetScriptingBackend(namedBuildTarget); |
| 53 | + var target = report.summary.platform; |
| 54 | + |
| 55 | + // 1) Force IL2CPP-only targets |
| 56 | + if (Il2CppOnlyTargets.Contains(target)) |
| 57 | + { |
| 58 | + SetBackendIfNeeded(namedBuildTarget, currentBackend, ScriptingImplementation.IL2CPP); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // 2) Force Mono-only targets |
| 63 | + if (MonoOnlyTargets.Contains(target)) |
| 64 | + { |
| 65 | + SetBackendIfNeeded(namedBuildTarget, currentBackend, ScriptingImplementation.Mono2x); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // 3) Ask for everything else (avoid dialogs in batch mode) |
| 70 | + bool useIl2Cpp; |
| 71 | + if (Application.isBatchMode) |
| 72 | + { |
| 73 | + // Safe default for CI: keep current backend (or change to true to default IL2CPP) |
| 74 | + useIl2Cpp = (currentBackend == ScriptingImplementation.IL2CPP); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + useIl2Cpp = EditorUtility.DisplayDialog( |
| 79 | + "Scripting Backend", |
| 80 | + $"Build target: {target}\n\nUse IL2CPP for this build?", |
| 81 | + "Yes (IL2CPP)", |
| 82 | + "No (Mono)" |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + SetBackendIfNeeded( |
| 87 | + namedBuildTarget, |
| 88 | + currentBackend, |
| 89 | + useIl2Cpp ? ScriptingImplementation.IL2CPP : ScriptingImplementation.Mono2x |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + private static void BumpVersionsIfNeeded(BuildTarget target) |
| 94 | + { |
| 95 | + if (AutoIncrementBundleVersion) |
| 96 | + { |
| 97 | + var before = PlayerSettings.bundleVersion; |
| 98 | + var after = IncrementBundleVersion(before); |
| 99 | + if (after != before) |
| 100 | + { |
| 101 | + PlayerSettings.bundleVersion = after; |
| 102 | + Debug.Log($"[Build] bundleVersion: {before} -> {after}"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Only bump Android versionCode when building Android (usually what you want). |
| 107 | + // If you want it bumped on *any* build, remove the target check. |
| 108 | + if (AutoIncrementAndroidVersionCode && target == BuildTarget.Android) |
| 109 | + { |
| 110 | + int before = PlayerSettings.Android.bundleVersionCode; |
| 111 | + int after = Mathf.Max(1, before + 1); |
| 112 | + PlayerSettings.Android.bundleVersionCode = after; |
| 113 | + Debug.Log($"[Build] Android versionCode: {before} -> {after}"); |
| 114 | + |
| 115 | + // Android versionName comes from PlayerSettings.bundleVersion by default. |
| 116 | + // If you want it explicitly logged: |
| 117 | + Debug.Log($"[Build] Android versionName: {PlayerSettings.bundleVersion}"); |
| 118 | + } |
| 119 | + |
| 120 | + // If you want the changes to definitely persist to ProjectSettings on disk: |
| 121 | + // AssetDatabase.SaveAssets(); |
| 122 | + } |
| 123 | + |
| 124 | + private static string IncrementBundleVersion(string version) |
| 125 | + { |
| 126 | + // Prefer X.Y.Z |
| 127 | + if (ForceSemanticVersionFormat) |
| 128 | + { |
| 129 | + // Match "major.minor.patch" with optional extra junk ignored |
| 130 | + var m = Regex.Match(version ?? "", @"^\s*(\d+)\.(\d+)\.(\d+)\s*$"); |
| 131 | + if (m.Success) |
| 132 | + { |
| 133 | + int major = int.Parse(m.Groups[1].Value); |
| 134 | + int minor = int.Parse(m.Groups[2].Value); |
| 135 | + int patch = int.Parse(m.Groups[3].Value) + 1; |
| 136 | + return $"{major}.{minor}.{patch}"; |
| 137 | + } |
| 138 | + |
| 139 | + // If it isn't semver, coerce it into semver and start at .0.1 |
| 140 | + // Examples: |
| 141 | + // "1" -> "1.0.1" |
| 142 | + // "1.2" -> "1.2.1" |
| 143 | + // "v1.2" -> "1.2.1" (extracts digits) |
| 144 | + var nums = Regex.Matches(version ?? "", @"\d+"); |
| 145 | + int majorC = nums.Count > 0 ? int.Parse(nums[0].Value) : 0; |
| 146 | + int minorC = nums.Count > 1 ? int.Parse(nums[1].Value) : 0; |
| 147 | + int patchC = 1; |
| 148 | + return $"{majorC}.{minorC}.{patchC}"; |
| 149 | + } |
| 150 | + |
| 151 | + // Otherwise, try to increment a trailing number, else append ".1" |
| 152 | + var match = Regex.Match(version ?? "", @"^(.*?)(\d+)\s*$"); |
| 153 | + if (match.Success) |
| 154 | + { |
| 155 | + string prefix = match.Groups[1].Value; |
| 156 | + int n = int.Parse(match.Groups[2].Value) + 1; |
| 157 | + return $"{prefix}{n}"; |
| 158 | + } |
| 159 | + |
| 160 | + if (string.IsNullOrWhiteSpace(version)) return "0.0.1"; |
| 161 | + return version.Trim() + ".1"; |
| 162 | + } |
| 163 | + |
| 164 | + private static void SetBackendIfNeeded( |
| 165 | + UnityEditor.Build.NamedBuildTarget namedBuildTarget, |
| 166 | + ScriptingImplementation current, |
| 167 | + ScriptingImplementation desired) |
| 168 | + { |
| 169 | + if (current == desired) return; |
| 170 | + PlayerSettings.SetScriptingBackend(namedBuildTarget, desired); |
| 171 | + } |
| 172 | +} |
0 commit comments