在使用Jenkins打包之前,我们需要将目前的工程进行一些准备。

上传SVN

将工程文件上传SVN。使用SVN——Commit。

注意:这里我们直接使用SVN——Commit,不要先使用SVN——Update,因为我们之前修改了框架内部的文件。在Commit的时候,注意将missing的文件也上传上去,并且把Assets文件夹内新添加的许多文件上传上去。

测试AB包加载

上传SVN后,我们先打一个AB包,然后在主工程AssetBundle文件夹中将内部的AB包放到C盘的C:\Users\zerwa\AppData\LocalLow\DefaultCompany\ResLoadPrg\Origin文件夹,这是因为我们在安卓环境下,AB包都是从这个目录加载的。如果不是安卓环境,需要把AB包放在StreamingAssets文件夹。

然后修改ResourceManagerm_LoadFromAssetBundle为true,进行测试

Jenkins修改配置

想要让Jenkins打热更包,需要修改之前的打包配置,这里以安卓打包为例,其他平台打包也是一样地修改

进入localhost:8080,点击ResLoadPrg_Android——点击“配置”。

添加parameter

  1. 添加一个“Boolean Parameter”,名称为“IsHotfix”,描述为:“是否热更”
  2. 添加一个“String Parameter”,名称为“HotVerPath”,默认值为:“E:\UnityProjects\ResLoadPrg_Android\Version”,描述为:“版本文件的路径”
  3. 添加一个“String Parameter”,名称为“HotCount”,默认值为:“1”,描述为:“当前版本的热更次数”

修改Build Steps —— Invoke Unity3D Editor

1
-projectpath %WorkPath% -quit -batchmode -executeMethod BuildApp.BuildAndroid Place=$Place Version=$Version Name=$Name Build=$Build MulRendering=$MulRendering IL2CPP=$IL2CPP Debug=$Debug IsHotfix=$IsHotfix HotVerPath=$HotVerPath HotCount=$HotCount -logFile "E:\unityAndroidJenkins.txt"

BuildStep

增加Build Steps——Execute Windows Batch Command

我们打热更包时,归档的不是apk文件,而是热更包,在这里我们必须添加压缩步骤

1
2
3
4
5
6
7
8
9
10
11
@echo off
if %IsHotfix%==true goto A

exit
:A
echo "IsHotfix"
c:
cd C:\Program Files (x86)\360\360zip
call 360zip.exe -ar "%WorkPath%\Hot\Android" "%WorkPath%\Hot\hotfix.zip"
exit 0
pause

如果是ResLoadPrg_PC的配置,我们在这里不需要添加别的步骤,而是要修改之前的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@echo off
if %IsHotfix%==true goto A
for /f "delims= " %%i in (%WorkPath%\buildname.txt) do set name=%%i
c:
cd C:\Program Files (x86)\360\360zip
call 360zip.exe -ar "%WorkPath%\BuildTarget\Windows\%name%" "%WorkPath%\BuildTarget\Windows\%name%.zip"
exit 0
pause

exit
:A
echo "IsHotfix"
c:
cd C:\Program Files (x86)\360\360zip
call 360zip.exe -ar "%WorkPath%\Hot\StandaloneWindows64" "%WorkPath%\Hot\hotfix.zip"
exit 0
pause

构建后操作

Archive the artifacts里面添加,Hot\*.zip

1
BuildTarget\Android\*.apk,Hot\*.zip

有多个归档文件时,使用逗号连接

Unity脚本配置(仅示例安卓)

打开Unity主工程,修改BuildApp脚本

BuildApp脚本的BuildSetting类里,添加新的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class BuildSetting
{
//...
/// <summary>
/// 是否IL2CPP
/// </summary>
public bool IL2CPP = false;
/// <summary>
/// 是否打热更包
/// </summary>
public bool IsHotfix = false;
/// <summary>
/// 版本文件的路径
/// </summary>
public string HotVerPath = string.Empty;
/// <summary>
/// 热更次数
/// </summary>
public int HotCount = 0;
}

修改BuildApp脚本的GetJenkinsAndroidBuildSetting方法,添加几行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// <summary>
/// 根据Jenkins的参数生成BuildSetting中间类
/// </summary>
static BuildSetting GetJenkinsAndroidBuildSetting()
{
string[] cmdArgs = Environment.GetCommandLineArgs();
BuildSetting buildSetting = new BuildSetting();
foreach (string arg in cmdArgs)
{
//...
else if (arg.StartsWith("IsHotfix"))
{
var tempParam = arg.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParam.Length == 2)
{
bool.TryParse(tempParam[1], out buildSetting.IsHotfix);
}
}
else if (arg.StartsWith("HotVerPath"))
{
var tempParam = arg.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParam.Length == 2)
{
buildSetting.HotVerPath = tempParam[1].Trim();
}
}
else if (arg.StartsWith("HotCount"))
{
var tempParam = arg.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (tempParam.Length == 2)
{
int.TryParse(tempParam[1].Trim(), out buildSetting.HotCount);
}
}
}
return buildSetting;
}

修改BuildApp脚本的BuildAndroid方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void BuildAndroid()
{
PlayerSettings.Android.keystoreName = Application.dataPath.Replace("/Assets", "") + "/android.keystore";
PlayerSettings.Android.keystorePass = "ataoandroid";
PlayerSettings.Android.keyaliasName = "resloadprgandr";
PlayerSettings.Android.keyaliasPass = "ataoandroid";

BuildSetting buildSetting = GetJenkinsAndroidBuildSetting();//+++
//打热更包
if(buildSetting.IsHotfix)//+++
{
BundleEditor.Build(true, buildSetting.HotVerPath, buildSetting.HotCount.ToString());
return;
}

//打AB包
BundleEditor.NormalBuild();
string suffix = SetAndroidSetting(buildSetting);
//生成可执行程序
//...
}

测试打包

在进行测试之前,修改一些工程内需要热更的文件。

然后Commit SVN。并关闭Unity。

进入Jenkins,按下图配置,点击开始构建

安卓热更打包配置