diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ccdb7a..ad0b342 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.13] + +### Added + +- Added support for handling build profiles in Unity 6 and above + ## [1.0.12] ### Added diff --git a/Editor/AzurePipelinesBuildTemplate.cs b/Editor/AzurePipelinesBuildTemplate.cs index 376a4cd..a040fe6 100644 --- a/Editor/AzurePipelinesBuildTemplate.cs +++ b/Editor/AzurePipelinesBuildTemplate.cs @@ -28,32 +28,28 @@ public static void PerformBuild() return; } - if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) +#if UNITY_6000_0_OR_NEWER + // If building using Unity 6 or above we check whether we are building using a build profile. + // When building using a build profile there is some settings we want to leave untouched as it is + // part of the build profile itself to define those settings. E.g. there may be a build profile + // for Android that outputs a AAB instead of APK and another one producing an APK file. We do not have to worry + // about it as the developer simply has to selected the right build profile depending on the wanted output. + var activeBuildProfile = UnityEditor.Build.Profile.BuildProfile.GetActiveBuildProfile(); + if (activeBuildProfile != null && activeBuildProfile) { - // When building for Android, check for custom keystore signing credentials and apply them prior to building the project. - if (Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystoreNameArgument, out var androidKeystoreName) && - Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystorePassArgument, out var androidKeystorePass) && - Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystoreAliasNameArgument, out var androidKeystoreAliasName)) - { - PlayerSettings.Android.useCustomKeystore = true; - PlayerSettings.Android.keystoreName = androidKeystoreName; - PlayerSettings.Android.keystorePass = androidKeystorePass; - PlayerSettings.Android.keyaliasName = androidKeystoreAliasName; - PlayerSettings.Android.keyaliasPass = Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystoreAliasPassArgument, out var androidKeystoreAliasPass) ? - androidKeystoreAliasPass : - androidKeystorePass; - } - else - { - // If no credentials passed, we assume the user wants to build using a development keystore auto-generated by Unity. - // We clear the custom keystore flag here in case developers have checked in a keystore config to the repository, which - // would cause the build to fail. - PlayerSettings.Android.useCustomKeystore = false; - } - - // When building for Android, we might want to build an .aab for the Play Store instead of an .apk file. - EditorUserBuildSettings.buildAppBundle = Dinomite.AzurePipelines.Utilities.CommandLineArgumentExists(AndroidBuildAppBundleArgument); + Debug.Log("BUILD INFO: Building using build profile flow."); + BuildUsingBuildProfile(activeBuildProfile, locationPathName, outputFileName); + return; } +#endif + + Debug.Log("BUILD INFO: Building using build target flow."); + BuildUsingBuildTarget(locationPathName, outputFileName); + } + + private static void BuildUsingBuildTarget(string locationPathName, string outputFileName) + { + CheckAndroidSettings(true); try { @@ -117,6 +113,75 @@ public static void PerformBuild() } } +#if UNITY_6000_0_OR_NEWER + private static void BuildUsingBuildProfile(UnityEditor.Build.Profile.BuildProfile buildProfile, string locationPathName, string outputFileName) + { + CheckAndroidSettings(false); + + try + { + var buildReport = BuildPipeline.BuildPlayer(new BuildPlayerWithProfileOptions + { + buildProfile = buildProfile, + locationPathName = Path.Combine(locationPathName, GetBuildTargetOutputFileNameAndExtension(outputFileName)), + options = BuildOptions.None + }); + + switch (buildReport.summary.result) + { + case BuildResult.Succeeded: + EditorApplication.Exit(0); + break; + case BuildResult.Unknown: + case BuildResult.Failed: + case BuildResult.Cancelled: + default: + Debug.LogError($"BUILD FAILED: {buildReport.summary.result}\n{buildReport.summary}"); + EditorApplication.Exit(1); + break; + } + } + catch (Exception ex) + { + Debug.LogError("BUILD FAILED: " + ex.Message); + EditorApplication.Exit(1); + } + } +#endif + + private static void CheckAndroidSettings(bool checkBuildAppBundle) + { + if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) + { + // When building for Android, check for custom keystore signing credentials and apply them prior to building the project. + if (Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystoreNameArgument, out var androidKeystoreName) && + Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystorePassArgument, out var androidKeystorePass) && + Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystoreAliasNameArgument, out var androidKeystoreAliasName)) + { + PlayerSettings.Android.useCustomKeystore = true; + PlayerSettings.Android.keystoreName = androidKeystoreName; + PlayerSettings.Android.keystorePass = androidKeystorePass; + PlayerSettings.Android.keyaliasName = androidKeystoreAliasName; + PlayerSettings.Android.keyaliasPass = Dinomite.AzurePipelines.Utilities.TryGetCommandLineArgumentValue(AndroidKeystoreAliasPassArgument, out var androidKeystoreAliasPass) ? + androidKeystoreAliasPass : + androidKeystorePass; + } + else + { + // If no credentials passed, we assume the user wants to build using a development keystore auto-generated by Unity. + // We clear the custom keystore flag here in case developers have checked in a keystore config to the repository, which + // would cause the build to fail. + PlayerSettings.Android.useCustomKeystore = false; + } + + if (checkBuildAppBundle) + { + // When building for Android, we might want to build an .aab for the Play Store instead of an .apk file. + EditorUserBuildSettings.buildAppBundle = Dinomite.AzurePipelines.Utilities.CommandLineArgumentExists(AndroidBuildAppBundleArgument); + } + } + } + private static string GetBuildTargetOutputFileNameAndExtension(string outputFileName) { switch (EditorUserBuildSettings.activeBuildTarget) diff --git a/package.json b/package.json index add51f6..b14f86a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "games.dinomite.azurepipelines", "displayName": "Dinomite.AzurePipelines", "description": "A Unity package containing build scripts for use with the Unity Tools for Azure DevOps extension.", - "version": "1.0.12", + "version": "1.0.13", "unity": "2018.1", "author": "Dinomite", "keywords": [