1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| static readonly string OUTPUT_PATH_NAME = "-PROJ_OUTPUT"; static readonly string PRODUCTT_NAME = "-PRODUCT_NAME"; //包名称
...
static string GetCommandArgValue(string key) { string defaultValue = string.Empty; // 获取参数的值 string[] args = System.Environment.GetCommandLineArgs(); for (int i = 0; i < args.Length - 1; i++) { if (args[i].Equals(key)) { defaultValue = args[i + 1]; break; } } return defaultValue; }
static string[] GetBuildScenes() { List<string> names = new List<string>();
foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes) { if (e == null) continue;
if (e.enabled) names.Add(e.path); } return names.ToArray(); }
static string GetBuildPathAndroid() { string dirPath = GetCommandArgValue(OUTPUT_PATH_NAME) + "/" + GetCommandArgValue(PRODUCTT_NAME) + ".apk"; if (!System.IO.Directory.Exists(dirPath)) { System.IO.Directory.CreateDirectory(dirPath); } return dirPath; }
static void CommandLineBuildAndroid() { Debug.Log("Command line build android version\n------------------\n------------------");
string[] scenes = GetBuildScenes(); string path = GetBuildPathAndroid(); if (scenes == null || scenes.Length == 0 || path == null) { Debug.LogError("Please add scene to buildsetting..."); return; }
Debug.Log(string.Format("Path: \"{0}\"", path)); for (int i = 0; i < scenes.Length; ++i) { Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i])); }
Debug.Log("Starting Android Build!"); SetUnityBuildArgs(BuildTargetGroup.Android); BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None); }
|