|
| 1 | +#if UNITY_EDITOR |
| 2 | + |
| 3 | +using UnityEngine; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEditor.SceneManagement; |
| 6 | +using AltTester.AltTesterUnitySDK.Editor; |
| 7 | +using AltTester.AltTesterUnitySDK; |
| 8 | +using System; |
| 9 | +using System.IO; |
| 10 | + |
| 11 | +public class AndroidBuilder |
| 12 | +{ |
| 13 | + private const string DefaultBuildPath = "Builds/Android/SampleApp.apk"; |
| 14 | + |
| 15 | + static void Build() |
| 16 | + { |
| 17 | + BuildPlayer(DefaultBuildPath, BuildOptions.Development); |
| 18 | + } |
| 19 | + |
| 20 | + static void BuildForAltTester() |
| 21 | + { |
| 22 | + BuildPlayer(DefaultBuildPath, BuildOptions.Development | BuildOptions.IncludeTestAssemblies, true); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + private static void BuildPlayer(string defaultBuildPath, BuildOptions buildOptions, bool setupForAltTester = false) |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + string buildPath = GetBuildPathFromArgs(defaultBuildPath); |
| 31 | + |
| 32 | + BuildPlayerOptions buildPlayerOptions = CreateBuildPlayerOptions(buildPath, buildOptions); |
| 33 | + |
| 34 | + if (setupForAltTester) |
| 35 | + { |
| 36 | + SetupAltTester(buildPlayerOptions); |
| 37 | + } |
| 38 | + |
| 39 | + var results = BuildPipeline.BuildPlayer(buildPlayerOptions); |
| 40 | + |
| 41 | + if (setupForAltTester) |
| 42 | + { |
| 43 | + // Clean up AltTester settings after build |
| 44 | + AltBuilder.RemoveAltTesterFromScriptingDefineSymbols(BuildTargetGroup.Android); |
| 45 | + RemoveAltFromScene(buildPlayerOptions.scenes[0]); |
| 46 | + } |
| 47 | + } |
| 48 | + catch (Exception exception) |
| 49 | + { |
| 50 | + Debug.LogException(exception); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static string GetBuildPathFromArgs(string defaultBuildPath) |
| 55 | + { |
| 56 | + string[] args = Environment.GetCommandLineArgs(); |
| 57 | + for (int i = 0; i < args.Length; i++) |
| 58 | + { |
| 59 | + if (args[i] == "--buildPath" && i + 1 < args.Length) |
| 60 | + { |
| 61 | + return args[i + 1]; |
| 62 | + } |
| 63 | + } |
| 64 | + return defaultBuildPath; |
| 65 | + } |
| 66 | + |
| 67 | + private static BuildPlayerOptions CreateBuildPlayerOptions(string buildPath, BuildOptions buildOptions) |
| 68 | + { |
| 69 | + return new BuildPlayerOptions |
| 70 | + { |
| 71 | + scenes = new[] |
| 72 | + { |
| 73 | + "Assets/Scenes/SelectAuthMethod.unity", |
| 74 | + "Assets/Scenes/UnauthenticatedScene.unity", |
| 75 | + "Assets/Scenes/AuthenticatedScene.unity", |
| 76 | + "Assets/Scenes/ZkEvmGetBalance.unity", |
| 77 | + "Assets/Scenes/ZkEvmGetTransactionReceipt.unity", |
| 78 | + "Assets/Scenes/ZkEvmSendTransaction.unity", |
| 79 | + "Assets/Scenes/ImxNftTransfer.unity" |
| 80 | + }, |
| 81 | + locationPathName = buildPath, |
| 82 | + target = BuildTarget.Android, |
| 83 | + options = buildOptions |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + private static void SetupAltTester(BuildPlayerOptions buildPlayerOptions) |
| 88 | + { |
| 89 | + AltBuilder.AddAltTesterInScriptingDefineSymbolsGroup(BuildTargetGroup.Android); |
| 90 | + AltBuilder.CreateJsonFileForInputMappingOfAxis(); |
| 91 | + |
| 92 | + var instrumentationSettings = new AltInstrumentationSettings(); |
| 93 | + AltBuilder.InsertAltInScene(buildPlayerOptions.scenes[0], instrumentationSettings); |
| 94 | + } |
| 95 | + |
| 96 | + public static void RemoveAltFromScene(string scene) |
| 97 | + { |
| 98 | + Debug.Log("Removing AltTesterPrefab from the [" + scene + "] scene."); |
| 99 | + |
| 100 | + var sceneToModify = EditorSceneManager.OpenScene(scene); |
| 101 | + |
| 102 | + // Find the AltTesterPrefab instance in the scene |
| 103 | + var altRunner = GameObject.FindObjectOfType<AltRunner>(); |
| 104 | + |
| 105 | + if (altRunner != null) |
| 106 | + { |
| 107 | + // Destroy the AltTesterPrefab instance |
| 108 | + GameObject.DestroyImmediate(altRunner.gameObject); |
| 109 | + |
| 110 | + // Mark the scene as dirty and save it |
| 111 | + EditorSceneManager.MarkSceneDirty(sceneToModify); |
| 112 | + EditorSceneManager.SaveOpenScenes(); |
| 113 | + |
| 114 | + Debug.Log("AltTesterPrefab successfully removed from the [" + scene + "] scene."); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + Debug.LogWarning("AltTesterPrefab was not found in the [" + scene + "] scene."); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +#endif |
0 commit comments