Unity3d通过脚本生成apk
2024-09-27 16:01:17
参考链接: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 > )
{
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 > )
{
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");
} }
最新文章
- ****LINUX命令(含GIT命令)个人总结
- Python 10 —— 杂
- Unity2.0容器自动注册机制
- Linux下LDAPSearch的例子
- WPF Button的背景图片设置
- windows 7 旗舰版 切换 中英文 界面
- Android系统--输入系统(十七)Dispatcher线程_分发dispatch
- vbs文件共享变量与函数的方法
- [poj2406]Power Strings_hash
- 想知道谁是你的最佳用户?基于Redis实现排行榜周期榜与最近N期榜
- 测者的性能测试手册:快速安装LoadRunner Linux上的Generator
- js备战春招の四のDOM
- Linux (麒麟)系统 重启后无法登陆进图形界面
- 微信小程序大型系统架构中应用Redis缓存要点
- 【Luogu3602】Koishi Loves Segments(贪心)
- OpenLayers学习笔记(一)—在线加载谷歌影像地图&;离线加载本地瓦片地图
- jmeter接口测试基础知识1.0
- Kubernetes部署SpringCloud(二) 部署ZUUL与服务 非host, 伸缩与负载均衡
- 逻辑回归 logit
- C#中泛型的解释(object,list,var,dynamic的区别)