跳到主要内容

并行构建 AB 包和 Player

下面的示例将展示 Windows 项目中并行构建 AB 包和 Player 的构建方法以及工作流文件。

构建方法

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

namespace AssetBundles
{
public class BuildScript
{
public static void BuildAssetBundles()
{
// Choose the output path according to the build target.
string outputPath = Path.Combine("AssetBundles", "Windows");
outputPath = Path.Combine("Assets/StreamingAssets",outputPath);
if (!Directory.Exists(outputPath) )
Directory.CreateDirectory (outputPath);

BuildPipeline.BuildAssetBundles (outputPath, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
}

public static void BuildScripts()
{
string[] levels = GetLevelsFromBuildSettings();
if (levels.Length == 0)
{
Debug.Log("Nothing to build.");
return;
}

BuildOptions option = EditorUserBuildSettings.development ? BuildOptions.Development : BuildOptions.None;

BuildPipeline.BuildPlayer(levels, "./Build/ScriptsOnly/test.exe", BuildTarget.StandaloneWindows64, option);
}

public static void BuildAssetBundle()
{
BuildAssetBundles();
}

static string[] GetLevelsFromBuildSettings()
{
List<string> levels = new List<string>();
for(int i = 0 ; i < EditorBuildSettings.scenes.Length; ++i)
{
if (EditorBuildSettings.scenes[i].enabled)
levels.Add(EditorBuildSettings.scenes[i].path);
}

return levels.ToArray();
}
}
}

工作流文件

name: CloudBuild Build Player And Bundle In Parallel

# 在提交至代码库时触发云构建任务
on: [push]

jobs:
build:
strategy:
matrix:
include:
- method: AssetBundles.BuildScript.BuildScripts
path: ./my_project/EndlessRunner_Openday/Build/ScriptsOnly
name: player
- method: AssetBundles.BuildScript.BuildAssetBundle
path: ./my_project/EndlessRunner_Openday/Assets/StreamingAssets
name: bundle
# 云构建runner标签,内部测试目前只支持如下标签(windows server 2022 with Tuanjie 2022.3.2t7)
runs-on: windows-server-2022-tuanjie-1.2.0-pc-16c-32g
steps:
# 从 PlasticSCM 签出项目
- name: Check out project repository
uses: actions/checkout-plasticscm@v1
with:
# 签出项目的目标路径, 默认为 runner 指定的 workspace 路径,设置后为 workspace/path 路径
path: my_project

- name: List repository content
run: ls -l ./my_project

# 构建
- name: Build project
id: build-action
uses: actions/tj-builder@v4
with:
# 指定构建方法
buildMethod: ${{ matrix.method }}
targetPlatform: StandaloneWindows64
# 团结项目路径
projectPath: ./my_project/EndlessRunner_Openday
enableLibraryCache: false

# 检查构建结果是否成功生成
- name: Check build result
run: ls -l ${{ matrix.path }}

- name: Check build dir
run: ls -l ./my_project

# 上传制品
- name: Upload artifact
uses: actions/tj-upload-artifact@v3
with:
# 这里使用构建步骤输出的 buildVersion 来标记制品名称
name: Build-${{ matrix.name }}
path: ${{ matrix.path }}
retention-days: 1

output:
runs-on: windows-server-2022-tuanjie-1.2.0-weixinminigame-8c-16g
needs: build
steps:
# 下载 Player
- name: Download Player Artifacts
uses: actions/download-artifact@v3
with:
name: Build-player

# 下载 Bundle
- name: Download Bundle Artifact
uses: actions/download-artifact@v3
with:
name: Build-bundle

# 解压制品并将 Bundle 移动到 Player 文件夹内
- name: Integrate Game
run: Expand-Archive ./Build-bundle.zip -DestinationPath ./StreamingAssets; Expand-Archive ./Build-player.zip -DestinationPath ./Game; cp -r ./StreamingAssets/ ./Game/test_Data/

# 上传游戏
- name: Upload Game
uses: actions/tj-upload-artifact@v3
with:
name: Build-Game
path: ./Game
retention-days: 1