# Build Automation 故障排除

> Solve common issues with Build Automation failures.

## 检查错误日志##check-the-logs-for-errors

对任何 Unity Build Automation (UBA) 问题进行故障排除的第一步是检查错误日志。

UBA 日志会以红色突出显示错误消息，以黄色突出显示警告消息，以提高消息的可见性。UBA 还会在精简日志选项卡中收集所有的警告和错误消息，以便于查找，但并非所有故障都会记录在精简日志中，因此检查完整日志可能会有所帮助。如果有多个错误日志，并且您之前有过成功的构建记录，则可以比较这些日志，以过滤掉成功构建过程中可能出现的任何错误。

## 运行全新构建##run-a-clean-build

如果 UBA 构建缓存库文件夹或工作空间文件夹，可能会导致问题。要强制目标忽略缓存，请在触发构建时使用 **Clean Build（全新构建）** 选项。

## 在本地以批处理模式运行##run-locally-in-batch-mode

如果项目在编辑器中能够成功构建，但却无法通过 Unity Build Automation 进行构建，这可能是因为您使用批处理模式来生成构建。为了确定错误是否会重现，可以尝试在本地以批处理模式运行

首先，您需要删除 `Library` 文件夹，或者将源代码签出到新的新文件夹，这会强制 UBA 重新下载相关包。这会模拟 UBA 构建服务器在每次构建中签出源代码的行为。

以下是同一命令的 Windows 和 macOS 版本。

> **Note:**
>
> **注意**：其中几个标志不是必需的。如果不想运行单元测试，可以跳过 `-testPlatform`、`-testResults` 和 `-runTests` 等标志。

所提供的目录是 Unity 编辑器的默认安装文件夹；请根据本地计算机上的正确路径进行调整。

### Windows CMD 控制台##windows-cmd-console

```text
"C:\Program Files\Unity\Hub\Editor\{UNITY_VERSION}\Editor\unity.exe" -batchMode -skipMissingProjectID -skipMissingUPID -buildTarget {BUILD_TARGET} -logFile C:\{SOME_PATH}\log.txt -projectPath C:\{PROJECT_PATH} -executeMethod {CLASS_AND_STATIC_METHOD_NAME_OF_BUILD_SCRIPT} -quit
```

### macOS 终端##macos-terminal

```text
/Applications/Unity/Hub/Editor/{UNITY_VERSION}/Unity.app/Contents/MacOS/Unity -batchMode -skipMissingProjectID -skipMissingUPID -buildTarget {BUILD_TARGET} -logFile /Users/{SOME_PATH}/log.txt -projectPath /Users/{PROJECT_PATH} -executeMethod {CLASS_AND_STATIC_METHOD_NAME_OF_BUILD_SCRIPT} -quit
```

如果还想运行测试，可以添加以下标签：

* `-runTests`
* `-testPlatform {PLATFORM} // playmode or editmode`
* `-testResults {FILE_PATH_AND_FILE_NAME}.xml`

### 模板构建脚本##template-build-script

将包含执行构建的静态方法的构建脚本放在 Editor 文件夹中。如果还没有 Editor 文件夹，请创建一个。

以下是供您参考的模板构建脚本：

```text
using UnityEditor;
class MyEditorScript
{
  static void PerformBuild ()
  {
    BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
    buildPlayerOptions.scenes = new[] {"Assets/Scenes/MyScene.unity", ...};
    buildPlayerOptions.target = BuildTarget.iOS; // More details on BuildTarget doc
    buildPlayerOptions.options = BuildOptions.None;
    buildPlayerOptions.locationPathName = "iOSBuild";
    BuildPipeline.BuildPlayer(buildPlayerOptions);
  }
}
```

## 常见问题##common-issues

一般问题：

* [构建立即失败](./builds-failing-immediately.md)
* [“项目无法识别”错误](./unrecognized-project-error.md)
* [使用 Git 子模块时克隆挂起](./git-clone-hangs.md)
* [光照贴图烘焙问题](./lightmap-baking.md)

特定于平台的问题：

* [Android 构建失败](./android-build-failures.md)
* [迁移到新版 Xcode 后出现错误](./error-migrating-xcode.md)
* [OSX 公证失败](./osx-notarization-failure.md)
* [iOS Build Automation 在 XCode 导出期间出现问题](./xcode-export-issues.md)
* [无法从 Unity Build Automation 下载或安装 iOS 构建](./ios-build-download-issue.md)
