关闭 UnityBuild

默认情况下,UE 默认开启了 bUseUnityBuild,会把多个 cpp 合并成一个翻译单元进行编译,加快项目的编译速度,编译时有*_1_of_8.cpp.obj 等 log。所以如果项目的头文件包含不规范,编译时头文件检测可能会出现问题:修改了 A 文件导致 B 文件出现错误。

所以,想在检测出中这种错误,可以关闭UnityBuild,使每个 cpp 都作为单独的翻译单元编译,有两种方法:

  1. 对整个工程(包含插件)关闭bUseUnityBuild,在项目的 Target.cs 中添加
target.cs
1
2
bForceUnityBuild = false;
bUseUnityBuild = false;

不修改代码,也可以在 BuildCongiguration.xml 中配置关闭:

1
2
3
4
5
6
7
8
9
10
11
12
13
/// <summary>
/// Whether to unify C++ code into larger files for faster compilation.
/// </summary>
[CommandLine("-DisableUnity", Value = "false")]
[XmlConfigFile(Category = "BuildConfiguration")]
public bool bUseUnityBuild = true;

/// <summary>
/// Whether to force C++ source files to be combined into larger files for faster compilation.
/// </summary>
[CommandLine("-ForceUnity")]
[XmlConfigFile(Category = "BuildConfiguration")]
public bool bForceUnityBuild = false;
  1. 对单个模块关闭UnityBuild,在 Build.cs 中关闭
1
bUseUnity = false;

可以在 Target.cs 中指定关闭 UnityBuild 的模块,也可以配置在 BuildConfiguration.xml 中:

1
2
3
4
5
/// <summary>
/// List of modules to disable unity builds for
/// </summary>
[XmlConfigFile(Category = "ModuleConfiguration", Name = "DisableUnityBuild")]
public string[] DisableUnityBuildForModules = null;

可以在 Intermediate\Build\Win64\UE4Editor\Development\Client 等路径下看到生成的大量 .response 文件。