|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEngine; |
| 8 | + |
| 9 | +namespace Oculus.Movement.Utils |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// This class contains useful menus used for generating sample builds. |
| 13 | + /// </summary> |
| 14 | + public class GenerateBuild |
| 15 | + { |
| 16 | + private static readonly string[] _sceneNames = |
| 17 | + new string[] { |
| 18 | + "t:scene, MovementAura", |
| 19 | + "t:scene, MovementBlendshapeMappingExample", |
| 20 | + "t:scene, MovementHighFidelity", |
| 21 | + "t:scene, MovementRetargeting", |
| 22 | + "t:scene, MovementBodyTrackingForFitness", |
| 23 | + "t:scene, MovementHipPinning", |
| 24 | + "t:scene, MovementISDKIntegration", |
| 25 | + "t:scene, MovementLocomotion" |
| 26 | + }; |
| 27 | + |
| 28 | + private const string _MAIN_BUILD_NAME = "movement"; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Builds an APK with as many samples as possible, depending on whether or not |
| 32 | + /// those samples have been imported into the Assets folder or not. |
| 33 | + /// </summary> |
| 34 | + [MenuItem("Movement/Build Samples APK", priority = 100)] |
| 35 | + public static void CreateSamplesBuildAPK() |
| 36 | + { |
| 37 | + List<string> validScenePaths = new List<string>(); |
| 38 | + foreach (string sceneName in _sceneNames) |
| 39 | + { |
| 40 | + var scenePath = PathOfAssetInAssetsFolder(sceneName); |
| 41 | + if (scenePath != String.Empty) |
| 42 | + { |
| 43 | + validScenePaths.Add(scenePath); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (validScenePaths.Count == 0) |
| 48 | + { |
| 49 | + Debug.LogError($"No samples scenes have been imported; cannot build."); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + GenerateAndroidBuild(validScenePaths.ToArray(), _MAIN_BUILD_NAME, |
| 54 | + "MovementSDK Samples", false); |
| 55 | + } |
| 56 | + |
| 57 | + private static string PathOfAssetInAssetsFolder(string assetName) |
| 58 | + { |
| 59 | + string[] guids = |
| 60 | + AssetDatabase.FindAssets(assetName, new string[] { "Assets" }); |
| 61 | + if (guids.Length == 0) |
| 62 | + { |
| 63 | + return String.Empty; |
| 64 | + } |
| 65 | + return AssetDatabase.GUIDToAssetPath(guids[0]); |
| 66 | + } |
| 67 | + |
| 68 | + private static void GenerateAndroidBuild(string[] buildScenes, string buildName, |
| 69 | + string productName, bool exitAfterBuild = true) |
| 70 | + { |
| 71 | + string previousAppIdentifier = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android); |
| 72 | + string previousProductName = PlayerSettings.productName; |
| 73 | + string targetAppId = "com.meta." + buildName; |
| 74 | + PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, targetAppId); |
| 75 | + PlayerSettings.Android.targetArchitectures = AndroidArchitecture.ARM64; |
| 76 | + PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP); |
| 77 | + bool prevForceSDCardPerm = PlayerSettings.Android.forceSDCardPermission; |
| 78 | + PlayerSettings.Android.forceSDCardPermission = false; |
| 79 | + PlayerSettings.productName = productName; |
| 80 | + BuildPlayerOptions buildOptions = new BuildPlayerOptions() |
| 81 | + { |
| 82 | + locationPathName = string.Format("builds/{0}.apk", buildName), |
| 83 | + scenes = buildScenes, |
| 84 | + target = BuildTarget.Android, |
| 85 | + targetGroup = BuildTargetGroup.Android, |
| 86 | + }; |
| 87 | + buildOptions.options = new BuildOptions(); |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + var error = BuildPipeline.BuildPlayer(buildOptions); |
| 92 | + RestorePreviousSettings(previousAppIdentifier, previousProductName, prevForceSDCardPerm); |
| 93 | + HandleBuildErrors.Check(error, exitAfterBuild, buildScenes); |
| 94 | + } |
| 95 | + catch |
| 96 | + { |
| 97 | + Debug.Log("Exception while building: exiting with exit code 2"); |
| 98 | + RestorePreviousSettings(previousAppIdentifier, previousProductName, prevForceSDCardPerm); |
| 99 | + EditorApplication.Exit(2); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static void RestorePreviousSettings(string previousAppIdentifier, string previousProductName, |
| 104 | + bool prevForceSDCardPerm) |
| 105 | + { |
| 106 | + PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, previousAppIdentifier); |
| 107 | + PlayerSettings.productName = previousProductName; |
| 108 | + PlayerSettings.Android.forceSDCardPermission = prevForceSDCardPerm; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Handles errors after build process. This can be used to catch the edge case where |
| 114 | + /// builds don't actually succeed even if they are marked as doing so. |
| 115 | + /// </summary> |
| 116 | + public static class HandleBuildErrors |
| 117 | + { |
| 118 | + /// <summary> |
| 119 | + /// Check for build error edge cases. |
| 120 | + /// </summary> |
| 121 | + /// <param name="buildReport">Build report.</param> |
| 122 | + /// <param name="exitAfterBuild">If we need to exit after the build or not.</param> |
| 123 | + /// <param name="scenesBuilt">Scenes built.</param> |
| 124 | + public static void Check(UnityEditor.Build.Reporting.BuildReport buildReport, bool exitAfterBuild, |
| 125 | + string[] scenesBuilt) |
| 126 | + { |
| 127 | + bool buildSucceeded = |
| 128 | + buildReport.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded; |
| 129 | + if (buildReport.summary.platform == BuildTarget.Android) |
| 130 | + { |
| 131 | + // Android can fail to produce the output even if the build is marked as succeeded in some rare |
| 132 | + // scenarios, notably if the Unity directory is read-only. This should be handled. |
| 133 | + buildSucceeded = buildSucceeded && File.Exists(buildReport.summary.outputPath); |
| 134 | + } |
| 135 | + if (buildSucceeded) |
| 136 | + { |
| 137 | + foreach (var scene in scenesBuilt) |
| 138 | + { |
| 139 | + Debug.Log($"Built scene {scene}."); |
| 140 | + } |
| 141 | + Debug.Log("Exiting with code 0. Success."); |
| 142 | + |
| 143 | + if (exitAfterBuild) |
| 144 | + { |
| 145 | + EditorApplication.Exit(0); |
| 146 | + } |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + Debug.Log("Exiting with code 1. Failure."); |
| 151 | + if (exitAfterBuild) |
| 152 | + { |
| 153 | + EditorApplication.Exit(1); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments