前言

unity打包有很多步骤,还有很多注意事项,但是在开发阶段,QA为了测试,一天就要打好几个包,要是一直是客户端人工去打包,那不得浪费一堆人力资源
自动打包应运而生,Jenkins 配合python完美将打包这件事交给QA,所以整个一键打包是非常有必要的,可以节约大家很多的时间。

目录

  • Jenkin包教包会
  • Bat文件编写

Jenkins使用篇

1. Jenkins的安装?

下载安装Jenkins,
下载地址在这里: https:// jenkins.io/download/
下载后直接解压安装即可

2.Jenkins的部署

下载安装完,就可以开始我们的一键打包部署了。




jenkins 打包 harbor jenkins 打包成blk文件_android unity 文件读写


jenkins 打包 harbor jenkins 打包成blk文件_Jenkins_02


jenkins 打包 harbor jenkins 打包成blk文件_自动打包_03


jenkins 打包 harbor jenkins 打包成blk文件_jenkins 打包 harbor_04

添加SSH

添加SSH的方法:在git项目下,右键打开gitbash,输入 ssh-keygen 然后输入文件名,回车,回车,就会在项目文件夹下生成文件名.pub的SSH文件,右键文本编辑器打开,到github仓库内Setting-Deploy keys-Add Key,然后黏贴进我们的SSH文件内容就好了。

配置保存好,一个简单的从GitHub上拉取工程的任务已经好了,构建下任务,也就是执行下我们创建的任务,你会惊喜的发现,github上的工程check下来了,目录在E:Jenkinsworkspace工程名,就是你安装Jenkins的路径,下面会自动创建工作区间workspace下级目录就是你的任务名称,所有关于本任务的操作都会在这个目录下!

3. 接下来我们来实现脚本自动构建项目。

①添加参数:ProjectPath 项目路径


jenkins 打包 harbor jenkins 打包成blk文件_android unity 文件读写_05


②添加步骤:执行Batch文件


jenkins 打包 harbor jenkins 打包成blk文件_Jenkins_06


cd /d %ProjectPath% 
BuildWithParameter.bat %Game_Name%



Batch脚本编写篇

③编写batch:实现切换目录到指定路径,调用unity打包接口

BuildWithParameter.bat 内容如下


REM UNITY程序的路径
SET UNITY_PATH="C:Program FilesUnity_5.6.6UnityEditorUnity.exe"
 
REM 游戏程序路径
SET PROJECT_PATH="E:JenkinsworkspaceAutoBuild"
 
REM 在Unity中构建apk
%UNITY_PATH% -projectPath %PROJECT_PATH% -quit -batchmode -executeMethod ProjectBuild.BuildForAndroid %1 -logFile build.log
 
echo "Apk生成完毕"
PAUSE


这个bat文件调用了unity项目的ProjectBuild.cs文件的BuildForAndroid方法


#region Copyright © 2018 Aver. All rights reserved.
/*
=====================================================
 AverFrameWork v1.0
 Filename:    ProjectBuild.cs
 Author:      Zeng Zhiwei
 Time:        2019/10/23 16:57:35
=====================================================
*/
#endregion

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

class ProjectBuild
{
    //在这里找出你当前工程所有的场景文件,假设你只想把部分的scene文件打包 那么这里可以写你的条件判断 总之返回一个字符串数组。
    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 void BuildForAndroid()
    {
        //todo 每次build删除之前的残留
        BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
        BuildTargetGroup targetGroup = BuildTargetGroup.Android;
        // 切换到对应平台
        EditorUserBuildSettings.SwitchActiveBuildTarget(targetGroup,target);

        string path = Application.dataPath + "/" + "test"+ ".apk";
        BuildOptions options = BuildOptions.AcceptExternalModificationsToPlayer;
        // 生成Android APK
        EditorUserBuildSettings.androidBuildSubtarget = MobileTextureSubtarget.ETC;
        EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;

        options |= BuildOptions.CompressWithLz4;
        BuildPipeline.BuildPlayer(GetBuildScenes(), path, target, options);
        Debug.LogFormat("----EndBuildAndroid---------- outputpath:{0}", path);
    }
}


注意

手动build一个包,有console的日志,调试起来比较方便,会有蛮多坑的,有SDK和NDK版本问题、Android Support等等。在手动能打出包后,再开始自动打包!!!

参考

Unity3D研究院之IOS全自动编辑framework、plist、oc代码(六十七) | 雨松MOMO程序研究院

Unity3D研究院之Android全自动打包生成apk(六十九) | 雨松MOMO程序研究院

Unity3D研究院之IOS全自动打包生成ipa(六十八) | 雨松MOMO程序研究院