参考链接:http://www.jianshu.com/p/a9261113b4ac
照着链接的方法并没有正确生成APK,IPA没有测试过,不过大致的方法是正确的,修改如下:
Environment.bat
:: set your own Unity path set unity="D:\Program Files\Unity\Editor\Unity.exe" :: -debug or -release set debugParam=-release set projectPath="E:\mycode\test\testpro"
UnityToApk.bat
call Environment.bat echo "Start Build Unity to Apk" %unity% -batchmode -projectPath %projectPath% -executeMethod CommandBuilder.PreBuild %debugParam% -quit -logFile ./PreBuild.log %unity% -batchmode -projectPath %projectPath% -executeMethod CommandBuilder.Build %debugParam% -android -quit -logFile ./BuildApk.log echo "End Build,please see log PreBuild.log and BuildApk.log"
cs脚本,放在Editor目录下
using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class CommandBuilder { private static string[] build_scenes = { "Assets/ddd.unity" }; private static bool ms_isDebugBuild = false; private static BuildTarget ms_buildTarget = BuildTarget.Android; private static string XCODE_PROJECT_NAME = "XCodeProject"; private static string BUILD_OUTPUT_ANDROID = Application.dataPath + "/Bin/Android/"; private static void UpdateBuildFlag() { string[] args = System.Environment.GetCommandLineArgs(); foreach (string oneArg in args) { if (oneArg != null && oneArg.Length > 0) { if (oneArg.ToLower().Contains("-debug")) { Debug.Log("\"-debug\" is detected, switch to debug build."); ms_isDebugBuild = true; return; } else if (oneArg.ToLower().Contains("-release")) { Debug.Log("\"-release\" is detected, switch to release build."); ms_isDebugBuild = false; return; } } } if (ms_isDebugBuild) { Debug.Log("neither \"-debug\" nor \"-release\" is detected, current is to debug build."); } else { Debug.Log("neither \"-debug\" nor \"-release\" is detected, current is to release build."); } } private static void UpdateBuildTarget() { string[] args = System.Environment.GetCommandLineArgs(); foreach (string oneArg in args) { if (oneArg != null && oneArg.Length > 0) { if (oneArg.ToLower().Contains("-android")) { Debug.Log("\"-android\" is detected, switch build target to android."); ms_buildTarget = BuildTarget.Android; return; } else if (oneArg.ToLower().Contains("-iphone")) { Debug.Log("\"-iphone\" is detected, switch build target to iphone."); ms_buildTarget = BuildTarget.iOS; return; } else if (oneArg.ToLower().Contains("-ios")) { Debug.Log("\"-ios\" is detected, switch build target to iphone."); ms_buildTarget = BuildTarget.iOS; return; } } } Debug.Log("neither \"-android\", \"-ios\" nor \"-iphone\" is detected, current build target is: " + ms_buildTarget); } public static void PreBuild() { Debug.Log("PreBuild"); UpdateBuildFlag(); } public static void Build() { Debug.Log("Build"); UpdateBuildTarget(); BuildOptions buildOption = BuildOptions.None; if (ms_isDebugBuild) { buildOption |= BuildOptions.Development; buildOption |= BuildOptions.AllowDebugging; buildOption |= BuildOptions.ConnectWithProfiler; } else { buildOption |= BuildOptions.None; } string locationPathName; if (BuildTarget.iOS == ms_buildTarget) { locationPathName = XCODE_PROJECT_NAME; } else { locationPathName = BUILD_OUTPUT_ANDROID; //原链接此处没有生成目录,而是使用rmdir mkdir生成,实际测试时无法将APK复制进目录中,所以讲目录修改如下 if (Directory.Exists(locationPathName)) Directory.Delete(locationPathName, true); Directory.CreateDirectory(locationPathName); System.DateTime time = System.DateTime.Now; locationPathName += "test_" + time.Month.ToString("D2") + time.Day.ToString("D2") + "_" + time.Hour.ToString("D2") + time.Minute.ToString("D2") + ".apk"; } BuildPipeline.BuildPlayer(build_scenes, locationPathName, ms_buildTarget, buildOption); } public static void PostBuild() { Debug.Log("PostBuild"); } }
时间: 2024-10-08 07:55:23