From 749c26c5bfa149601e8731c5b5fb45b46fd87604 Mon Sep 17 00:00:00 2001 From: Natalie Bunduwongse Date: Wed, 14 Aug 2024 10:10:20 +1200 Subject: [PATCH] chore: mac build script to build without alttester --- sample/Assets/Editor/MacBuilder.cs | 108 ++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 18 deletions(-) diff --git a/sample/Assets/Editor/MacBuilder.cs b/sample/Assets/Editor/MacBuilder.cs index 1261631a..646a3bee 100644 --- a/sample/Assets/Editor/MacBuilder.cs +++ b/sample/Assets/Editor/MacBuilder.cs @@ -2,18 +2,71 @@ using UnityEngine; using UnityEditor; +using UnityEditor.SceneManagement; using AltTester.AltTesterUnitySDK.Editor; using AltTester.AltTesterUnitySDK; using System; -public class BuildScript +public class MacBuilder { + private const string DefaultBuildPath = "Builds/MacOS/SampleApp.app"; + + static void Build() + { + BuildPlayer(DefaultBuildPath, BuildOptions.None); + } + static void BuildForAltTester() + { + BuildPlayer(DefaultBuildPath, BuildOptions.Development | BuildOptions.IncludeTestAssemblies | BuildOptions.AutoRunPlayer, true); + } + + private static void BuildPlayer(string defaultBuildPath, BuildOptions buildOptions, bool setupForAltTester = false) { try { - BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); - buildPlayerOptions.scenes = new string[] { + string buildPath = GetBuildPathFromArgs(defaultBuildPath); + BuildPlayerOptions buildPlayerOptions = CreateBuildPlayerOptions(buildPath, buildOptions); + + if (setupForAltTester) + { + SetupAltTester(buildPlayerOptions); + } + + var results = BuildPipeline.BuildPlayer(buildPlayerOptions); + + if (setupForAltTester) + { + // Clean up AltTester settings after build + AltBuilder.RemoveAltTesterFromScriptingDefineSymbols(BuildTargetGroup.Standalone); + RemoveAltFromScene(buildPlayerOptions.scenes[0]); + } + } + catch (Exception exception) + { + Debug.LogException(exception); + } + } + + private static string GetBuildPathFromArgs(string defaultBuildPath) + { + string[] args = Environment.GetCommandLineArgs(); + for (int i = 0; i < args.Length; i++) + { + if (args[i] == "--buildPath" && i + 1 < args.Length) + { + return args[i + 1]; + } + } + return defaultBuildPath; + } + + private static BuildPlayerOptions CreateBuildPlayerOptions(string buildPath, BuildOptions buildOptions) + { + return new BuildPlayerOptions + { + scenes = new[] + { "Assets/Scenes/SelectAuthMethod.unity", "Assets/Scenes/UnauthenticatedScene.unity", "Assets/Scenes/AuthenticatedScene.unity", @@ -21,29 +74,48 @@ static void BuildForAltTester() "Assets/Scenes/ZkEvmGetTransactionReceipt.unity", "Assets/Scenes/ZkEvmSendTransaction.unity", "Assets/Scenes/ImxNftTransfer.unity" - }; + }, + locationPathName = buildPath, + target = BuildTarget.StandaloneOSX, + options = buildOptions + }; + } - buildPlayerOptions.locationPathName = "Builds/MacOS/SampleApp.app"; - buildPlayerOptions.target = BuildTarget.StandaloneOSX; - buildPlayerOptions.options = BuildOptions.Development | BuildOptions.IncludeTestAssemblies | BuildOptions.AutoRunPlayer; + private static void SetupAltTester(BuildPlayerOptions buildPlayerOptions) + { + AltBuilder.AddAltTesterInScriptingDefineSymbolsGroup(BuildTargetGroup.Standalone); + AltBuilder.CreateJsonFileForInputMappingOfAxis(); - // Setup for AltTester - var buildTargetGroup = BuildTargetGroup.Standalone; - AltBuilder.AddAltTesterInScriptingDefineSymbolsGroup(buildTargetGroup); - if (buildTargetGroup == UnityEditor.BuildTargetGroup.Standalone) - AltBuilder.CreateJsonFileForInputMappingOfAxis(); - var instrumentationSettings = new AltInstrumentationSettings(); - AltBuilder.InsertAltInScene(buildPlayerOptions.scenes[0], instrumentationSettings); + var instrumentationSettings = new AltInstrumentationSettings(); + AltBuilder.InsertAltInScene(buildPlayerOptions.scenes[0], instrumentationSettings); + } - var results = BuildPipeline.BuildPlayer(buildPlayerOptions); - AltBuilder.RemoveAltTesterFromScriptingDefineSymbols(BuildTargetGroup.Standalone); + public static void RemoveAltFromScene(string scene) + { + Debug.Log("Removing AltTesterPrefab from the [" + scene + "] scene."); + + var sceneToModify = EditorSceneManager.OpenScene(scene); + + // Find the AltTesterPrefab instance in the scene + var altRunner = GameObject.FindObjectOfType(); + + if (altRunner != null) + { + // Destroy the AltTesterPrefab instance + GameObject.DestroyImmediate(altRunner.gameObject); + // Mark the scene as dirty and save it + EditorSceneManager.MarkSceneDirty(sceneToModify); + EditorSceneManager.SaveOpenScenes(); + + Debug.Log("AltTesterPrefab successfully removed from the [" + scene + "] scene."); } - catch (Exception exception) + else { - Debug.LogException(exception); + Debug.LogWarning("AltTesterPrefab was not found in the [" + scene + "] scene."); } } + } #endif