unity-build-pipeline
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity Build Pipeline
Unity Build Pipeline
Configure, script, and automate Unity 6 player builds: scenes, platform target, scripting
backend, stripping, and headless/CI builds. Targets Unity 6 (6000.0 LTS).
配置、编写脚本并自动化Unity 6玩家端构建:场景、平台目标、脚本后端、代码剥离,以及无头/CI构建。目标版本为Unity 6 (6000.0 LTS)。
When to use
适用场景
- Use when setting up Build Settings/Profiles, choosing a platform and scripting backend
(Mono vs IL2CPP), reducing build size with managed stripping, scripting a repeatable build
with , or wiring a CI/headless build.
BuildPipeline.BuildPlayer - Use when the project has or a CI build script.
ProjectSettings/EditorBuildSettings.asset
When not to use: authoring a CI service config end-to-end is DevOps; this skill covers
the Unity-side build API and settings. Console/platform certification specifics are
platform-NDA territory. Storefront submission → / .
steam-publishitch-publish- 适用于设置构建配置/参数、选择平台与脚本后端(Mono vs IL2CPP)、通过托管代码剥离缩小包体、使用编写可重复执行的构建脚本,或配置CI/无头构建时。
BuildPipeline.BuildPlayer - 适用于项目包含或CI构建脚本的场景。
ProjectSettings/EditorBuildSettings.asset
不适用场景: 端到端编写CI服务配置属于DevOps范畴;本技能仅覆盖Unity端的构建API与设置。主机/平台认证细节属于平台保密协议内容。商店提交请使用 / 。
steam-publishitch-publishCore workflow
核心工作流程
- List the scenes to build (File → Build Profiles/Settings → Scene List, or
). Only listed, enabled scenes ship; scene 0 is the start scene.
EditorBuildSettings.scenes - Pick the platform target and switch the active build target if needed
(/
BuildTarget).EditorUserBuildSettings - Choose the scripting backend (Player Settings): Mono (fast iteration, desktop) vs IL2CPP (AOT C++; required for many platforms, better perf, harder to reverse). IL2CPP needs the platform's C++ toolchain installed.
- Tune size/perf: set Managed Stripping Level (Disabled → Minimal → Low → Medium → High)
and protect reflection-only code with a . Set Quality Settings per platform.
link.xml - Script the build with and inspect the returned
BuildPipeline.BuildPlayer(BuildPlayerOptions)— a non-BuildReportresult must fail your pipeline.Succeeded - Run headless for CI with , and check the exit code.
-batchmode -quit -executeMethod - Verify the actual output runs (launch the player), not just that the build returned without throwing.
- 列出需构建的场景(文件→构建配置/设置→场景列表,或)。仅已列出且启用的场景会被打包;场景0为启动场景。
EditorBuildSettings.scenes - 选择平台目标,必要时切换活跃构建目标(/
BuildTarget)。EditorUserBuildSettings - 选择脚本后端(玩家设置):Mono(迭代速度快,适用于桌面平台) vs IL2CPP(AOT C++;为众多平台所需,性能更优,反编译难度大)。IL2CPP需要安装对应平台的C++工具链。
- 调整包体大小/性能:设置托管代码剥离级别(Disabled → Minimal → Low → Medium → High),并使用保护仅通过反射调用的代码。为各平台设置质量参数。
link.xml - 使用编写构建脚本,并检查返回的
BuildPipeline.BuildPlayer(BuildPlayerOptions)——若结果非BuildReport,必须终止流水线。Succeeded - 用于CI的无头运行:使用参数,并检查退出码。
-batchmode -quit -executeMethod - 验证:确保实际输出的玩家端可正常运行(启动玩家端),而非仅确认构建未抛出错误。
Patterns
模式示例
1. Scripted build with a result check
1. 带结果检查的脚本化构建
csharp
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
public static class BuildScript
{
[MenuItem("Build/Windows x64")]
public static void BuildWindows()
{
var options = new BuildPlayerOptions
{
scenes = new[] { "Assets/Scenes/Main.unity", "Assets/Scenes/Level1.unity" },
locationPathName = "Builds/Windows/Game.exe",
target = BuildTarget.StandaloneWindows64,
options = BuildOptions.None, // add BuildOptions.Development for a dev build
};
BuildReport report = BuildPipeline.BuildPlayer(options);
BuildSummary summary = report.summary;
if (summary.result != BuildResult.Succeeded)
throw new System.Exception($"Build failed: {summary.totalErrors} errors");
Debug.Log($"Build OK: {summary.totalSize} bytes in {summary.totalTime}");
}
}csharp
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
public static class BuildScript
{
[MenuItem("Build/Windows x64")]
public static void BuildWindows()
{
var options = new BuildPlayerOptions
{
scenes = new[] { "Assets/Scenes/Main.unity", "Assets/Scenes/Level1.unity" },
locationPathName = "Builds/Windows/Game.exe",
target = BuildTarget.StandaloneWindows64,
options = BuildOptions.None, // 若为开发构建,添加BuildOptions.Development
};
BuildReport report = BuildPipeline.BuildPlayer(options);
BuildSummary summary = report.summary;
if (summary.result != BuildResult.Succeeded)
throw new System.Exception($"构建失败:{summary.totalErrors} 个错误");
Debug.Log($"构建完成:{summary.totalSize} 字节,耗时{summary.totalTime}");
}
}2. Headless / CI invocation
2. 无头/CI调用
bash
undefinedbash
undefinedExit code is 0 on success; -quit ensures the editor closes; -nographics for build servers.
成功时退出码为0;-quit确保编辑器关闭;-nographics用于构建服务器。
Unity -batchmode -quit -nographics
-projectPath "/path/to/Project"
-executeMethod BuildScript.BuildWindows
-logFile -
-projectPath "/path/to/Project"
-executeMethod BuildScript.BuildWindows
-logFile -
undefinedUnity -batchmode -quit -nographics
-projectPath "/path/to/Project"
-executeMethod BuildScript.BuildWindows
-logFile -
-projectPath "/path/to/Project"
-executeMethod BuildScript.BuildWindows
-logFile -
undefined3. Protect stripped code with link.xml
link.xml3. 使用link.xml
保护被剥离的代码
link.xmlxml
<!-- Assets/link.xml — keep types the linker can't see are used (reflection, JSON, plugins). -->
<linker>
<assembly fullname="MyGameRuntime" preserve="all"/>
</linker>xml
<!-- Assets/link.xml — 保留链接器无法识别使用情况的类型(反射、JSON、插件)。 -->
<linker>
<assembly fullname="MyGameRuntime" preserve="all"/>
</linker>Pitfalls
常见陷阱
- A scene loads in the Editor but is missing in the build — it isn't in the Build Settings
scene list (or is disabled). only sees listed scenes.
SceneManager.LoadScene - IL2CPP build fails on a fresh machine — the platform C++ toolchain (e.g. Windows build tools, Android NDK) isn't installed. Mono has no such requirement.
- /
MissingMethodExceptiononly in the build — managed stripping removed reflection-only code. Lower the stripping level or add aTypeLoadExceptionpreserve entry.link.xml - Treating "BuildPlayer returned" as success — always check ; it can return with errors.
BuildReport.summary.result - Addressables content is stale/missing — Addressables () need a separate content build (Build → Addressables) and a profile pointing at the right load path; a player build alone doesn't rebuild them.
com.unity.addressables - Shipping a Development build — enables the profiler/debugging and is slower; use
BuildOptions.Developmentfor release.BuildOptions.None
- 编辑器中可加载场景,但构建包中缺失——该场景未加入构建设置的场景列表(或已被禁用)。仅能识别已列出的场景。
SceneManager.LoadScene - 新机器上IL2CPP构建失败——未安装对应平台的C++工具链(如Windows构建工具、Android NDK)。Mono无此要求。
- 仅在构建包中出现/
MissingMethodException——托管代码剥离移除了仅通过反射调用的代码。降低剥离级别或在TypeLoadException中添加保留条目。link.xml - 将“BuildPlayer返回”视为成功——务必检查;即使返回结果也可能存在错误。
BuildReport.summary.result - Addressables内容过时/缺失——Addressables ()需要单独进行内容构建(构建→Addressables),且需配置指向正确加载路径的参数;仅玩家端构建不会重新生成Addressables内容。
com.unity.addressables - 发布开发构建包——会启用分析器/调试功能,且运行速度较慢;发布版本请使用
BuildOptions.Development。BuildOptions.None
References
参考资料
- For a complete multi-platform CI build script (target switching, version stamping,
argument parsing, exit codes) and an Addressables content-build call, read
.
references/ci-build-script.md - Primary docs: , Unity Manual build sections (player settings, managed code stripping).
ScriptReference/BuildPipeline.BuildPlayer
- 如需完整的多平台CI构建脚本(目标切换、版本标记、参数解析、退出码)以及Addressables内容构建调用,请阅读。
references/ci-build-script.md - 主要文档:、Unity手册构建章节(玩家设置、托管代码剥离)。
ScriptReference/BuildPipeline.BuildPlayer
Related skills
相关技能
- /
steam-publish— distributing the player you just built.itch-publish - — editor scripting conventions used by build scripts.
unity-csharp-scripting
- /
steam-publish—— 发布你刚构建好的玩家端。itch-publish - —— 构建脚本所使用的编辑器脚本规范。
unity-csharp-scripting