Last active
May 10, 2021 16:28
-
-
Save nekomimi-daimao/e9fe411cc5ad14f8700e7adb06a883a1 to your computer and use it in GitHub Desktop.
Customizable Unity CLI build command script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if UNITY_EDITOR | |
| using System; | |
| using System.Collections.Generic; | |
| using UnityEditor; | |
| using UnityEditor.Build.Reporting; | |
| /// <summary> | |
| /// /opt/unity/Editor/Unity -projectPath "" -batchmode -nographics -quit -executeMethod BuildExecutor.Build | |
| /// <see cref="ParseArg"/> | |
| /// </summary> | |
| public static partial class BuildExecutor | |
| { | |
| #region Build | |
| public static void Build() | |
| { | |
| var args = ParseArg(); | |
| BuildInternal(args); | |
| } | |
| private static void BuildInternal(IReadOnlyDictionary<string, string> args) | |
| { | |
| var buildPlayerOptions = new BuildPlayerOptions(); | |
| InitializeBuildPlayerOption(ref buildPlayerOptions, in args); | |
| var buildReport = BuildPipeline.BuildPlayer(buildPlayerOptions); | |
| switch (buildReport.summary.result) | |
| { | |
| case BuildResult.Succeeded: | |
| // NOP | |
| break; | |
| case BuildResult.Unknown: | |
| case BuildResult.Failed: | |
| case BuildResult.Cancelled: | |
| default: | |
| EditorApplication.Exit(1); | |
| break; | |
| } | |
| } | |
| #endregion | |
| #region Partial | |
| static partial void InitializeBuildPlayerOption( | |
| ref BuildPlayerOptions buildPlayerOptions, | |
| in IReadOnlyDictionary<string, string> args | |
| ); | |
| #endregion | |
| #region ParseArg | |
| private const string ExecuteMethod = "-executeMethod"; | |
| /// <summary> | |
| /// -optionKey optionValue -> (optionKey, optionValue) | |
| /// -optionKey -> (optionKey, null) | |
| /// </summary> | |
| private static IReadOnlyDictionary<string, string> ParseArg() | |
| { | |
| var args = Environment.GetCommandLineArgs(); | |
| var index = Array.FindIndex(args, s => string.Equals(s, ExecuteMethod)); | |
| if (index < 0) | |
| { | |
| throw new IndexOutOfRangeException(); | |
| } | |
| const string prefixOption = "-"; | |
| var parsedArg = new Dictionary<string, string>(); | |
| for (var count = index + 2; count < args.Length; count++) | |
| { | |
| var rawArg = args[count]; | |
| if (string.IsNullOrEmpty(rawArg) || !rawArg.StartsWith(prefixOption)) | |
| { | |
| continue; | |
| } | |
| string optionValue = null; | |
| if (count + 1 < args.Length) | |
| { | |
| var rawValue = args[count + 1]; | |
| if (!string.IsNullOrEmpty(rawValue) && !rawValue.StartsWith(prefixOption)) | |
| { | |
| optionValue = rawValue; | |
| } | |
| } | |
| parsedArg[rawArg.Substring(prefixOption.Length)] = optionValue; | |
| } | |
| return parsedArg; | |
| } | |
| #endregion | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if UNITY_EDITOR | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using UnityEditor; | |
| public static partial class BuildExecutor | |
| { | |
| #region Partial | |
| static partial void InitializeBuildPlayerOption( | |
| ref BuildPlayerOptions buildPlayerOptions, | |
| in IReadOnlyDictionary<string, string> args | |
| ) | |
| { | |
| buildPlayerOptions.target = DetectTarget(args); | |
| buildPlayerOptions.scenes = EditorBuildSettings.scenes | |
| .Where(scene => scene.enabled) | |
| .Select(scene => scene.path) | |
| .ToArray(); | |
| buildPlayerOptions.locationPathName = args[OptionOutput]; | |
| if (args.ContainsKey(OptionDebug)) | |
| { | |
| buildPlayerOptions.options |= BuildOptions.Development; | |
| buildPlayerOptions.options |= BuildOptions.AllowDebugging; | |
| } | |
| } | |
| #endregion | |
| #region ParseArg | |
| private const string OptionDebug = "debug"; | |
| private const string OptionOutput = "out"; | |
| private static readonly Dictionary<string, BuildTarget> BuildTargets | |
| = new Dictionary<string, BuildTarget> | |
| { | |
| {"android", BuildTarget.Android}, | |
| {"ios", BuildTarget.iOS}, | |
| {"webgl", BuildTarget.WebGL}, | |
| }; | |
| // -target android | |
| private const string OptionTarget = "target"; | |
| private static BuildTarget DetectTarget(IReadOnlyDictionary<string, string> args) | |
| { | |
| if (args.TryGetValue(OptionTarget, out var rawTarget) | |
| && BuildTargets.TryGetValue(rawTarget.ToLower(), out var value)) | |
| { | |
| return value; | |
| } | |
| return BuildTarget.NoTarget; | |
| } | |
| #endregion | |
| #region EditorMenu | |
| [MenuItem("Tools/Build/Android")] | |
| private static void BuildAndroid() | |
| { | |
| var dic = new Dictionary<string, string>(); | |
| dic[OptionTarget] = "android"; | |
| BuildInternal(dic); | |
| } | |
| [MenuItem("Tools/Build/iOS")] | |
| private static void BuildiOS() | |
| { | |
| var dic = new Dictionary<string, string>(); | |
| dic[OptionTarget] = "ios"; | |
| BuildInternal(dic); | |
| } | |
| [MenuItem("Tools/Build/WebGL")] | |
| private static void BuildWebGL() | |
| { | |
| var dic = new Dictionary<string, string>(); | |
| dic[OptionTarget] = "webgl"; | |
| BuildInternal(dic); | |
| } | |
| #endregion | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment