首先创建一个start.bat脚本内容如下
@echo off
F:
cd F:\unityProject\Test
start cmd /c call build.bat
G:
cd G:\AndroidStudioProject\Test
./gradlew clean project
pause
start cmd /c call build.bat --新建一个CMD窗口执行另外一个.bat文件
cd G:\AndroidStudioProject\Test --进入到AndroidStudioProject文件夹中
./gradlew clean project --执行gradle的清除缓存命令
然后创建一个 build.bat文件 内容如下
TASKLIST /V /S localhost /U %username%>tmp_process_list.txt
TYPE tmp_process_list.txt |FIND "Unity.exe"
IF ERRORLEVEL 0 (GOTO UNITY_IS_RUNNING)
ELSE (GOTO START_UNITY)
:UNITY_IS_RUNNING
TASKKILL /F /IM Unity.exe
PING 127.0.0.1 -n 1 >NUL
GOTO START_UNITY
:START_UNITY
C:
CD C:\Program Files\Unity\Editor
.\Unity.exe -projectPath F:\unityProject\Test -quit -batchmode -executeMethod Test.BuildEditor.BuildApp
echo F | xcopy "F:\unityProject\Test\App\test\unityLibrary\libs\unity-classes.jar" "G:\AndroidStudioProject\Test\unityLibrary\libs\unity-classes.jar" /Y
set "notCopy="
set src=F:\unityProject\Test\App\test\unityLibrary\src\main\Il2CppOutputProject
set dest=G:\AndroidStudioProject\Test\unityLibrary\src\main\Il2CppOutputProject
cd /d %src%
for /f "tokens=*" %%i in ('dir /B %src%') do (
echo %notCopy%|findstr "%%i">nul||(echo %%~ai|findstr "d">nul && xcopy %%~fi\* %dest%\%%~ni\ /E /Y || xcopy %%~fi %dest%\ /S /Y)
)
set src1=F:\unityProject\Test\App\test\unityLibrary\src\main\assets
set dest1=G:\AndroidStudioProject\Test\unityLibrary\src\main\assets
cd /d %src1%
for /f "tokens=*" %%i in ('dir /B %src1%') do (
echo %notCopy%|findstr "%%i">nul||(echo %%~ai|findstr "d">nul && xcopy %%~fi\* %dest1%\%%~ni\ /E /Y || xcopy %%~fi %dest1%\ /S /Y)
)
set src2=F:\unityProject\Test\App\test\unityLibrary\src\main\jniLibs
set dest2=G:\AndroidStudioProject\Test\unityLibrary\src\main\jniLibs
cd /d %src2%
for /f "tokens=*" %%i in ('dir /B %src2%') do (
echo %notCopy%|findstr "%%i">nul||(echo %%~ai|findstr "d">nul && xcopy %%~fi\* %dest2%\%%~ni\ /E /Y || xcopy %%~fi %dest2%\ /S /Y)
)
set src3=F:\unityProject\Test\App\test\unityLibrary\src\main\jniStaticLibs
set dest3=G:\AndroidStudioProject\Test\unityLibrary\src\main\jniStaticLibs
cd /d %src3%
for /f "tokens=*" %%i in ('dir /B %src3%') do (
echo %notCopy%|findstr "%%i">nul||(echo %%~ai|findstr "d">nul && xcopy %%~fi\* %dest3%\%%~ni\ /E /Y || xcopy %%~fi %dest3%\ /S /Y)
)
cd /
G:
cd G:\AndroidStudioProject\Test
./gradlew assembleRelease
进入到Unity的安装目录Editor文件夹
我这个是用的Unity的破解版本 如果使用的UnityHub的话修改为如下
CD C:\Program Files\Unity\Hub\Editor\2020.1.5f1c1\Editor
启动.\Unity.exe 调用你要打包的Unity项目目录Editor文件夹下的Editor函数Test.BuildEditor.BuildApp Test是命名空间有则写 没有则去掉 BuildEditor是脚本名字 BuildApp是函数名字
拷贝和替换AS内容
进入到AS项目根目录里面然后 执行Release出包 也可以执行./gradlew assembleDebug 出Debug包
最后执行打开 G:\AndroidStudioProject\Test\launcher\build\outputs\apk\release\ 文件夹即可看到出的Apk
Unity脚本如下
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
namespace Test
{
public class BuildEditor
{
[MenuItem("Build/Build")]
public static void BuildApp()
{
BuildGameRes();
#if UNITY_ANDROID
Build_Apk();
#else
Build_Ipa();
#endif
}
private static void BuildGameRes()
{
//写自己的资源打包
}
private static void Build_Apk()
{
PlayerSettings.Android.keystoreName = Application.dataPath.Replace("/Assets", "") + "/test.keystore";
PlayerSettings.Android.keystorePass = "123456";
PlayerSettings.Android.keyaliasName = "test";
PlayerSettings.Android.keyaliasPass = "123456";
PlayerSettings.Android.useAPKExpansionFiles = false;
PlayerSettings.bundleVersion = "1";
PlayerSettings.Android.bundleVersionCode = 1;
PlayerSettings.applicationIdentifier = "com.test.test01";
PlayerSettings.Android.targetArchitectures = AndroidArchitecture.All;
string time = DateTime.Now.ToString("yyyyMMddHHmmss");
string[] scenes = {
@"Assets/Res/Resources/scene/test.unity",
};
string fileName = "test";
fileName = Application.dataPath.Replace("/Assets", "") + "/App/" + fileName;
string dir = Path.GetDirectoryName(fileName);
if (Directory.Exists(dir))
Directory.Delete(dir, true);
Directory.CreateDirectory(dir);
BuildOptions buildOptions = BuildOptions.CompressWithLz4;
BuildReport report = BuildPipeline.BuildPlayer(scenes, fileName, BuildTarget.Android, buildOptions);
if (report.summary.result != BuildResult.Succeeded)
{
Debug.LogError("build fail - " + fileName);
return;
}
AssetDatabase.Refresh();
string path = Application.dataPath.Replace("/Assets", "") + "/App";
path = path.Replace("/", @"\");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "explorer.exe";
p.StartInfo.Arguments = path;
p.Start();
Debug.LogError("build success - " + fileName);
}
private static void Build_Ipa()
{
PlayerSettings.bundleVersion = "1";
PlayerSettings.iOS.buildNumber = "1";
PlayerSettings.applicationIdentifier = "com.test.test01";
string[] scenes = {
@"Assets/Res/Resources/scene/test.unity",
};
string path = EditorUtility.SaveFolderPanel("save xcode", "", "");
BuildOptions buildOptions = BuildOptions.CompressWithLz4;
BuildReport report = BuildPipeline.BuildPlayer(scenes, path, BuildTarget.iOS, buildOptions);
if (report.summary.result != BuildResult.Succeeded)
{
Debug.LogError("build fail - " + path);
return;
}
AssetDatabase.Refresh();
Debug.LogError("build success - " + path);
}
}
}
参考链接:
使用命令行运行unity并执行某个静态函数(运用于命令行打包和批量打包)_林新发的博客-CSDN博客_unity 命令行打包
Unity命令行参数链接:文章来源:https://www.toymoban.com/news/detail-533131.html
命令行参数 - Unity 手册文章来源地址https://www.toymoban.com/news/detail-533131.html
到了这里,关于Unity 使用.bat自动化打包Apk的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!