| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEditor; |
| | 4 | | using UnityEditor.Build.Reporting; |
| | 5 | |
|
| | 6 | | static class BuildCommand |
| | 7 | | { |
| | 8 | | static string GetArgument(string name) |
| | 9 | | { |
| 0 | 10 | | string[] args = Environment.GetCommandLineArgs(); |
| | 11 | |
|
| 0 | 12 | | for (int i = 0; i < args.Length; i++) |
| | 13 | | { |
| 0 | 14 | | if (args[i].Contains(name)) |
| | 15 | | { |
| 0 | 16 | | return args[i + 1]; |
| | 17 | | } |
| | 18 | | } |
| | 19 | |
|
| 0 | 20 | | return null; |
| | 21 | | } |
| | 22 | |
|
| | 23 | | static string[] GetEnabledScenes() |
| | 24 | | { |
| | 25 | | // Get enabled scenes. |
| 0 | 26 | | List<string> enabledScenesList = new List<string>(); |
| 0 | 27 | | for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) |
| | 28 | | { |
| 0 | 29 | | if (EditorBuildSettings.scenes[i].enabled) |
| | 30 | | { |
| 0 | 31 | | enabledScenesList.Add(EditorBuildSettings.scenes[i].path); |
| | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| | 35 | | // Transform enabled scenes list into an array to be returned. |
| 0 | 36 | | string[] enabledScenesArray = new string[enabledScenesList.Count]; |
| 0 | 37 | | for (int i = 0; i < enabledScenesArray.Length; i++) |
| | 38 | | { |
| 0 | 39 | | enabledScenesArray[i] = enabledScenesList[i]; |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | return enabledScenesArray; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | static BuildTarget GetBuildTarget() |
| | 46 | | { |
| 0 | 47 | | string buildTargetName = GetArgument("customBuildTarget"); |
| 0 | 48 | | Console.WriteLine(":: Received customBuildTarget " + buildTargetName); |
| | 49 | |
|
| 0 | 50 | | return ToEnum<BuildTarget>(buildTargetName, BuildTarget.NoTarget); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | static string GetBuildPath() |
| | 54 | | { |
| 0 | 55 | | string buildPath = GetArgument("customBuildPath"); |
| 0 | 56 | | Console.WriteLine(":: Received customBuildPath " + buildPath); |
| | 57 | |
|
| 0 | 58 | | if (buildPath == "") |
| | 59 | | { |
| 0 | 60 | | throw new Exception("customBuildPath argument is missing"); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | return buildPath; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | static string GetBuildName() |
| | 67 | | { |
| 0 | 68 | | string buildName = GetArgument("customBuildName"); |
| 0 | 69 | | Console.WriteLine(":: Received customBuildName " + buildName); |
| | 70 | |
|
| 0 | 71 | | if (buildName == "") |
| | 72 | | { |
| 0 | 73 | | throw new Exception("customBuildName argument is missing"); |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | return buildName; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | static string GetFixedBuildPath(BuildTarget buildTarget, string buildPath, string buildName) |
| | 80 | | { |
| 0 | 81 | | if (buildTarget.ToString().ToLower().Contains("windows")) |
| | 82 | | { |
| 0 | 83 | | buildName = buildName + ".exe"; |
| 0 | 84 | | } |
| 0 | 85 | | else if (buildTarget.ToString().ToLower().Contains("webgl")) |
| | 86 | | { |
| | 87 | | // webgl produces a folder with index.html inside, there is no executable name for this buildTarget |
| 0 | 88 | | buildName = ""; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | return buildPath + buildName; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | static BuildOptions GetBuildOptions() |
| | 95 | | { |
| 0 | 96 | | string buildOptions = GetArgument("customBuildOptions"); |
| | 97 | |
|
| 0 | 98 | | return buildOptions == "AcceptExternalModificationsToPlayer" |
| | 99 | | ? BuildOptions.AcceptExternalModificationsToPlayer |
| | 100 | | : BuildOptions.None; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // https://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value |
| | 104 | | static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue) |
| | 105 | | { |
| 0 | 106 | | if (!Enum.IsDefined(typeof(TEnum), strEnumValue)) |
| | 107 | | { |
| 0 | 108 | | return defaultValue; |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | return (TEnum) Enum.Parse(typeof(TEnum), strEnumValue); |
| | 112 | | } |
| | 113 | |
|
| | 114 | | static string getEnv(string key, bool secret = false, bool verbose = true) |
| | 115 | | { |
| 0 | 116 | | var env_var = Environment.GetEnvironmentVariable(key); |
| | 117 | |
|
| 0 | 118 | | if (verbose) |
| | 119 | | { |
| 0 | 120 | | if (env_var != null) |
| | 121 | | { |
| 0 | 122 | | if (secret) |
| | 123 | | { |
| 0 | 124 | | Console.WriteLine(":: env['" + key + "'] set"); |
| 0 | 125 | | } |
| | 126 | | else |
| | 127 | | { |
| 0 | 128 | | Console.WriteLine(":: env['" + key + "'] set to '" + env_var + "'"); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | | else |
| | 132 | | { |
| 0 | 133 | | Console.WriteLine(":: env['" + key + "'] is null"); |
| | 134 | | } |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | return env_var; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | static void PerformBuild() |
| | 141 | | { |
| 0 | 142 | | Console.WriteLine(":: Performing build"); |
| | 143 | |
|
| 0 | 144 | | var buildTarget = GetBuildTarget(); |
| 0 | 145 | | var buildPath = GetBuildPath(); |
| 0 | 146 | | var buildName = GetBuildName(); |
| 0 | 147 | | var fixedBuildPath = GetFixedBuildPath(buildTarget, buildPath, buildName); |
| | 148 | |
|
| 0 | 149 | | if (buildTarget.ToString().ToLower().Contains("webgl")) |
| | 150 | | { |
| 0 | 151 | | PlayerSettings.WebGL.emscriptenArgs = " --profiling-funcs "; |
| | 152 | | } |
| | 153 | |
|
| 0 | 154 | | var buildSummary = BuildPipeline.BuildPlayer(GetEnabledScenes(), fixedBuildPath, buildTarget, GetBuildOptions()) |
| 0 | 155 | | Console.WriteLine(":: Done with build process"); |
| | 156 | |
|
| 0 | 157 | | if (buildSummary.summary.result != BuildResult.Succeeded) |
| | 158 | | { |
| 0 | 159 | | throw new Exception("The build was not successful"); |
| | 160 | | } |
| 0 | 161 | | } |
| | 162 | | } |