【Jenkins】实现Unity远程自动化打包

一、介绍

1、Jenkins

Jenkins 是一个开源的持续集成 / 持续交付(CI/CD)自动化工具,主要用来自动化构建、测试、打包、部署软件项目。

2、安装

(1)JDK(Java环境)安装,根据Jenkins提示安装对应依赖的版本

下载链接:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

判断JDK是否安装成功:打开管理员:Windows PowerShell面板

在管理员:Windows PowerShell面板,运行命令java -version,JDK安装成功显示:

(2)Jenkins安装(正常使用LTS版本,比较稳定)

下载链接:https://www.jenkins.io/

3、常用操作

(1)启动Jenkins

在管理员:Windows PowerShell面板,运行命令net start jenkins,如下:

打开http://localhost:1717,1717为自己设置的端口号,创建并登录自己的账号,如下:

(2)关闭Jenkins

在管理员:Windows PowerShell面板,运行命令net stop jenkins,如下:

(3)安装插件,以Unity3d插件为例子




4、简单demo实现

执行一个带参数的任务







二、Unity使用Jenkins实现远程自动化打包实例

1、流程

2、Unity打包工具实现

(1)创建一个Android平台的默认项目

(2)编写打包工具代码

csharp 复制代码
// BuildTools.cs
using UnityEngine;
using UnityEditor;

public class BuildTools
{
    [MenuItem("Build/Build APK")]
    public static void BuildApk()
    {
        // 解析命令行参数
        string[] args = System.Environment.GetCommandLineArgs();
        foreach (var s in args)
        {
            if (s.Contains("--productName:"))
            {
                string productName= s.Split(':')[1];
                // 设置app名字
                PlayerSettings.productName = productName;
            }

            if (s.Contains("--version:"))
            {
                string version = s.Split(':')[1];
                // 设置版本号
                PlayerSettings.bundleVersion = version;
            }
        }
       
        // 执行打包
        BuildPlayerOptions opt = new BuildPlayerOptions();
        opt.scenes = new string[] { "Assets/Scenes/SampleScene.unity" };
        opt.locationPathName = Application.dataPath + "/../Bin/test.apk";
        opt.target = BuildTarget.Android;
        opt.options = BuildOptions.None;

        BuildPipeline.BuildPlayer(opt);

        Debug.Log("Build App Done!");
    }
}

(3)测试是否能够正常打出包

成功导出:

(4)使用bat脚本,调用unity打包工具

bash 复制代码
::判断Unity是否运行中
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
::杀掉Unity
TASKKILL /F /IM Unity.exe
::停1秒
PING 127.0.0.1 -n 1 >NUL
GOTO START_UNITY

:START_UNITY
:: 此处执行Unity打包
"D:\software\Unity\2021.1.7f1c1\Editor\Unity.exe" ^
-quit ^
-batchmode ^
-projectPath "E:\UnityProject\UnityDemo" ^
-executeMethod BuildTools.BuildApk ^
-logFile "E:\UnityProject\UnityDemo\output.log" ^
--productName:%1 ^
--version:%2

(5)在Jenkins上调用bat脚本






3、注意点

(1)需要确保unity编辑器有许可证,否则会一直打包失败

相关推荐
Filotimo_2 小时前
在前端开发中,Jenkins 的作用
运维·jenkins
oMcLin2 小时前
如何在 RHEL 8.5 上部署并优化 Jenkins CI/CD 流水线,提升跨平台自动化部署的速度与稳定性?
ci/cd·自动化·jenkins
oMcLin2 小时前
如何在CentOS 7.9上配置并调优Docker Compose与Traefik,实现电商平台的自动化路由与高可用负载均衡?
docker·centos·自动化
天空属于哈夫克34 小时前
体验至上:如何打磨 RPA 外部群自动化的“人机交互”温度?
自动化·人机交互·rpa
OidEncoder11 小时前
从 “粗放清扫” 到 “毫米级作业”,编码器重塑环卫机器人新能力
人工智能·自动化·智慧城市
控制迷思14 小时前
电位器实现电机绝对位置测量-PART-电位计-绝对位置
自动化·simulink
AIGCExplore18 小时前
Jenkins 全局配置及工具验证教程
java·servlet·jenkins
Wpa.wk18 小时前
性能测试工具 - JMeter工具组件介绍二
运维·经验分享·测试工具·jmeter·自动化·json