在个人的Unrealengine账户中关联自己的GitHub账户成功之后,就可以访问UE4引擎的源码了。

git clone -b release https://github.com/EpicGames/UnrealEngine.git

克隆成功后,执行Setup.bat拉取并安装外部依赖文件(第三方库、资源包、示例工程等),最后调用UnrealVersionSelector添加引擎到注册表并在资源管理器中添加引擎快捷菜单功能

"%EngineDir%\Engine\Binaries\Win64\UnrealVersionSelector-Win64-Shipping.exe" /register

若需要设置代理可以传入相应参数来执行Setup.bat脚本 // Setup.bat会调用Engine\Binaries\DotNET\GitDependencies.exe工具来获取依赖文件

Setup.bat --proxy="http://10.125.224.93:8082" --force --exclude=Win32

注1: 如果是UE4.22版本,需要安装 .net framework 4.6.2 ,安装完后在控制面板卸载列表中会有如下软件列表:

注2: 如果Setup出现如下错误,需要下载替换对应的 Engine/Build/Commit.gitdeps.xml 文件

admin@NICOCHEN-MC4 UnrealEngine % -/Setup.sh
Registering git hooks... (this will override existing ones!)
Running system mono/msbuild,version: Mono JIT compiler version 6.12.0.182 (tarbal1 Mon Jan 16 23:26:18 UTC 2023)
Checking dependencies..
Updating dependencies:% (/63488).-.
Failed to download 'http://cdn.unrealengine.com/dependencies/UnrealEngine-12372779-01515af26c634d2a8ade60b1afd1f065/01bb78539fc8dda386d45f9b5615f9a1e8ca5d94':The remote server returned an error: (403) Forbidden. (WebException)

https://github.com/EpicGames/UnrealEngine/releases?page=2

执行GenerateProjectFiles.bat生成UE4相关工程,生成的vs工程文件在Engine\Intermediate\ProjectFiles目录中

双击UE4.sln,开始编译相关工具和引擎 // 编译的中间文件生成在EngineSource\Engine\Intermediate\Build目录中

用来存放UE4引擎和项目代码的目录的一些注意点:

1. 目录的全路径中不要包含中文、空格

2. 目录不要放置得太深,会导致编译链接的命令行太长的错误 注: 可通过在模块的Build.cs中设置 bLegacyPublicIncludePaths = false 来缓解

3. 目录名要以英文或下划线开头(不能以数字开头):因为UHT会利用目录名做为其符号名( 注: c++中变量名、函数名不能以数字开头)的一部分

编译UnrealBuildTool

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" "%EngineDir%\Engine\Source\Programs\UnrealBuildTool\UnrealBuildTool.csproj" /t:build /p:Configuration=Development;Platform=AnyCPU;TargetFrameworkVersion=v4.5

mac下使用mono提供的xbuild工具编译UnrealBuildTool

/Users/<用户>/Engine/Binaries/ThirdParty/Mono/Mac/bin/xbuild /Users/<用户>/Engine/Source/Programs/UnrealBuildTool/UnrealBuildTool.csproj /property:Configuration="Development" /verbosity:quiet /nologo

注1: 当UnrealBuildTool不存在或其源码有修改时,执行引擎目录下的GenerateProjectFiles.bat或GenerateProjectFiles.sh会重新生成UnrealBuildTool

注2: 在macOS上可以使用 Visual Studio for Mac 来打开csproj项目进行调试(macOS没有sln解决方案文件)

编译引擎工具

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" ShaderCompileWorker Win64 Development -waitmutex -FromMsBuild -DEPLOY  // Windows下编译ShaderCompileWorker
"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" UnrealLightmass Win64 Development -waitmutex -FromMsBuild -DEPLOY  // Windows下编译UnrealLightmass
"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" UnrealIdentifierRegister Win64 Development -waitmutex -FromMsBuild -DEPLOY  // Windows下编译UnrealIdentifierRegister
"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" UnrealVersionSelector Win64 Development -waitmutex -FromMsBuild -DEPLOY  // Windows下编译UnrealVersionSelector
mono $EngineDir/Engine/Binaries/DotNET/UnrealBuildTool.exe ShaderCompileWorker Mac Development  // Mac下编译ShaderCompileWorker

在Xcode中编译UnrealInsights,报如下错误:

Cannot code sign because the target does not have an Info.plist file and one is not being generated automatically. 
Apply an Info.plist file to the target using the INFOPLIST_FILE build setting or generate one automatically by setting the GENERATE_INFOPLIST_FILE build setting to YES (recommended).

使用ShaderCompilerWorker.exe编译shader

%EngineDir%/Engine/Binaries/Win64/ShaderCompileWorker.exe <AppData>/Local/Temp/UnrealShaderWorkingDir/BFE18CAA45FA658BF12AE489BE247311/5/ 27472 5 WorkerInputOnly.in WorkerOutputOnly.out -communicatethroughfile  -TimeToLive=20.000000 -Multiprocess

使用UnrealVersionSelector添加引擎到注册表并在资源管理器上下文菜单中添加引擎快捷功能

"%EngineDir%\Engine\Binaries\DotNET\UnrealVersionSelector.exe" /register

1. 对uproject文件进行右键菜单注册

2. 注册uproject文件所用的引擎路径

编译引擎和项目

编译Development版本的UE4引擎 // 会在%EngineDir%\Binaries\Win64和%EngineDir%\Plugins\*\···\*\Binaries\Win64目录中生成引擎UE4Editor.exe、核心dll模块和引擎插件dll

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" UE4Editor Win64 Development -waitmutex -FromMsBuild -DEPLOY

注1: 发生重编会在开始处打印如下log,log文件在UnrealEngine\Engine\Programs\UnrealBuildTool\Log.txt目录中

TargetMakefile.Load: Makefile is older than UnrealBuildTool.exe, ignoring it
BuildMode.CreateMakefile: Creating makefile for MyGameEditor (UnrealBuildTool.exe is newer)
// --------------------------------------------------------------
Invalidating makefile for UnrealHeaderTool (Definitions.h modified)
// --------------------------------------------------------------
TargetMakefile.Load: Makefile is older than .uproject file, ignoring it
BuildMode.CreateMakefile: Creating makefile for MyGameEditor (.uproject file is newer)

注2: UBT使用ActionGraph分析模块的代码是否需要Outdated,然后执行lib、pdb的清理工作

ActionGraph.IsActionOutdated: PCH.Core.cpp: Dependency GenericPlatformMisc.h is newer than the last execution of the action: 2023/2/2 21:26:10 vs 2023/1/31 9:47:55
ActionGraph.IsActionOutdated: SharedPCH.Slate.ShadowErrors.cpp: Dependency GenericPlatformMisc.h is newer than the last execution of the action: 2023/2/2 21:26:10 vs 2023/1/31 9:48:11
ActionGraph.IsActionOutdated: UE4Editor-Core.lib: Prerequisite PCH.Core.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: Module.TraceDataFiltering.cpp: Prerequisite SharedPCH.Slate.ShadowErrors.cpp is produced by outdated action.
。。。 。。。
ActionGraph.IsActionOutdated: UE4Editor-NetworkFile.dll: Prerequisite SharedPCH.CoreUObject.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: UE4Editor-PlayTimeLimit.dll: Prerequisite SharedPCH.CoreUObject.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: UE4Editor-ReliabilityHandlerComponent.dll: Prerequisite SharedPCH.Core.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: Module.PlayTimeLimit.cpp: Prerequisite SharedPCH.CoreUObject.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: UE4Editor-TakeRecorderSources.lib: Prerequisite SharedPCH.UnrealEd.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: UE4Editor-GeometryCacheAbcFile.lib: Prerequisite SharedPCH.UnrealEd.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: UE4Editor-DataValidation.lib: Prerequisite SharedPCH.UnrealEd.ShadowErrors.cpp is produced by outdated action.
ActionGraph.IsActionOutdated: UE4Editor-Gauntlet.lib: Prerequisite SharedPCH.Engine.ShadowErrors.cpp is produced by outdated action.
ActionGraph.DeleteOutdatedProducedItems: Deleting outdated item: D:\svn\UnrealEngine\Engine\Intermediate\Build\Win64\UE4Editor\Development\Core\UE4Editor
-Core.lib ActionGraph.DeleteOutdatedProducedItems: Deleting outdated item: D:\svn\UnrealEngine\Engine\Intermediate\Build\Win64\UE4Editor\Development\CoreUObject\UE4Editor-CoreUObject.lib 。。。 。。。 ActionGraph.DeleteOutdatedProducedItems: Deleting outdated item: D:\svn\UnrealEngine\Engine\Binaries\Win64\UE4Editor-Engine.pdb ActionGraph.DeleteOutdatedProducedItems: Deleting outdated item: D:\svn\UnrealEngine\Engine\Binaries\Win64\UE4Editor-UnrealEd.pdb

注3: 对比UnrealEngine\Engine\Intermediate\Build\Win64\UnrealHeaderTool\DebugGame\UnrealHeaderTool中的 Definitions.h Definitions.h.old 文件来决定是否需要重编译

生成了UE4.sln或UE4.xcworkspace,也可以使用msbuild.exe或xcodebuild来编译

// vs2017的msbuild所在目录:Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin
msbuild.exe UE4.sln /p:Configuration="Development Editor" /p:Platform=Win64 /m:8  // 使用并发线程数为8,编译配置为:Development Editor Win64来编译生成UE4Editor.exe
xcodebuild -workspace UE4.xcworkspace/ -scheme UE4 -configuration "Development Editor" // xcode下编译Development Editor的UE4Editor

xcode编译出的UE4Editor所在目录:%EngineDir%/Binaries/Mac/ UE4Editor.app /Contents/MacOS/UE4Editor

生成项目MyGame的项目工程文件 // 工程文件会生成到"%GameDir%\Intermediate\ProjectFiles"目录中

"%EngineDir%\Engine\Binaries\Win64\UnrealVersionSelector.exe" /projectfiles "%GameDir%\MyGame.uproject"   //windows下
"%EngineDir%/Engine/Binaries/DotNET/UnrealBuildTool.exe"  -projectfiles -project="%GameDir%\MyGame.uproject" -game -engine -progress -log="%GameDir%/Saved/Logs/UnrealVersionSelector-2020.12.31-11.34.19.log"  //windows下
"%EngineDir%/Engine/Binaries/DotNET/UnrealBuildTool.exe"  -projectfiles -project="%GameDir%\MyGame.uproject" -game -engine -VSCode   //windows下生成vscode项目工程
"%EngineDir%/Engine/Binaries/DotNET/UnrealBuildTool.exe"  -projectfiles -project="%GameDir%\MyGame.uproject" -game -engine -2019  //windows下生成vs2019项目工程  注:vs2019使用的c++14
"%EngineDir%/GenerateProjectFiles.sh" -project="%GameDir%/MyGame.uproject" -Game -Engine // Mac或Linux下
"%EngineDir%/GenerateProjectFiles.sh" -project="%GameDir%/MyGame.uproject" -platforms=IOS -game -nointellisense -IOSdeployonly -ignorejunk -projectfileformat=XCode -includetemptargets  // Mac下专有IOS工程

注: windows下,可在UE4编辑器菜单File -- Refresh Visual Studio Project来生成或刷新vs项目文件;在Mac下,可在UE4编辑器菜单File -- Refresh XCode Project来生成或刷新xCode项目文件      【与上面命令是等价的】

编译项目MyGame的Development Editor版本 // 会在%GameDir%\Binaries\Win64和%GameDir%\Plugins\*\···\*\Binaries\Win64目录中生成项目dll和项目插件dll

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" MyGameEditor Win64 Development "%GameDir%\MyGame.uproject" -waitmutex -FromMsBuild -DEPLOY // windows下
$EngineDir/Engine/Build/BatchFiles/Mac/XcodeBuild.sh MyGameEditor macosx Development %GameDir%/MyGame.uproject // Mac下
mono $EngineDir/Engine/Binaries/DotNET/UnrealBuildTool.exe  MyGameEditor macosx Development %GameDir%/MyGame.uproject // Mac下

编译日志UnrealEngine\Engine\Programs\UnrealBuildTool\Log.txt中会输出uplugin的依赖关系:

// 日志开头处,会打印插件是怎么被Enable开启的引用关系链
UEBuildTarget.AddPlugin: Enabling plugin 'GeometryProcessing' (referenced via MyGame.uproject -> MeshPainting.uplugin) // GeometryProcessing插件被开启:因为UAGame.uproject依赖MeshPainting插件,MeshPainting插件又依赖GeometryProcessing插件
UEBuildTarget.AddPlugin: Enabling plugin 'AndroidDeviceProfileSelector' (referenced via MyGame.uproject)  // AndroidDeviceProfileSelector插件被开启:因为UAGame.uproject依赖AndroidDeviceProfileSelector插件

编译项目MyGame的Development Android版本(libUE4.so)

UnrealBuildTool.exe  MyGame Android Development -Project=%GameDir%\MyGame.uproject"

编译项目MyGame的Client Development Android版本(libUE4.so)

UnrealBuildTool.exe  MyGameClient Android Development -Project=%GameDir%\MyGame.uproject" -Architectures=arm64

编译项目MyGame的Debug IOS版本(MyGame可执行二进制)

mono $EngineDir$/Engine/Binaries/DotNET/UnrealBuildTool.exe MyGame IOS Debug -Project=$GameDir$/MyGame.uproject

遇到的问题:ld: can't open -unexported_symbols_list file: /Users/xxx/Trunk/UAGroup/MyGame/unexported_symbols.txt

解决方法:在该目录下放置一个unexported_symbols.txt文件,里面内容如下:

*TSS*
*Tss*
*tss*
*AceSDK*
*MRPCS*
*mrpcs*
*tp2*
*tcj*
*_proto*
*ns_tinyxml*

编译单个c/cpp文件

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe"  MyGameEditor Win64 Development "%GameDir%\MyGame.uproject" -singlefile="%GameDir%\Source\MyGame\MyGameCharacter.cpp" -WaitMutex -FromMsBuild -DEPLOY

编译单个模块

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" -Module=MyGame Win64 Development -TargetType=Editor -Project="%GameDir%\MyGame.uproject" -canskiplink "%GameDir%\MyGame.uproject"

编译单个引擎模块

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" -Module=Engine Win64 Development -TargetType=Editor -Project="%GameDir%\MyGame.uproject" -canskiplink -nosharedpch "%GameDir%\MyGame.uproject"

编译多个模块

"%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" -Module=MyCommon -Module=MyGame Win64 Development -TargetType=Editor -Project="%GameDir%\MyGame.uproject" -canskiplink "%GameDir%\MyGame.uproject" 

注1: 更多额外命令行参数见UnrealBuildTool中的GlobalOptions类中的CommandLine修饰的成员变量

-Verbose、-VeryVerbose、-Log、-Timestamps、-Progress、-NoMutex、-RemoteIni、-SkipDeploy、-Clean、

-ProjectFiles、-ProjectFileFormat=、-Makefile、-CMakefile、-QMakefile、-KDevelopfile、-CodeliteFiles、-XCodeProjectFiles、-EdditProjectFiles、-VSCode、-VSMac、-CLion

注2: 更多额外命令行参数见UnrealBuildTool中的TargetDescriptor类中的CommandLine修饰的成员变量

-NoHotReload、-ForceHotReload、-LiveCoding、 -WriteActions=c:\myAction.txt (输出编译链接过程中详细信息)

-mode=JsonExport (读取所有build.cs,输出modules的依赖关系)、-LiveCodingModules=modulelist.txt、-LiveCodingManifest=mymenifest.txt、-Quiet

// 编译链接出so文件,另外记录编译链接各命令行的日志文件会输出在c:\svn\myAction.txt
C:\svn\UnrealEngine\Engine\Binaries\DotNET\UnrealBuildTool.exe  UAGame Android Shipping -Project="C:\svn\R6Game\UAGame.uproject" -WriteActions="c:\svn\myAction.txt"
// 不会编译链接出so文件,json文件会输出在C:\svn\R6Game\Binaries\Android\UAGame-Android-Shipping.json
C:\svn\UnrealEngine\Engine\Binaries\DotNET\UnrealBuildTool.exe  UAGame Android Shipping -Project="C:\svn\R6Game\UAGame.uproject" -mode=JsonExport

编译项目MyGame的客户端版本

"%EngineDir%\Engine\Binaries\Win64\UnrealVersionSelector.exe" MyGame Win64 Development -Project="%GameDir%\MyGame.uproject"

编译项目MyGame的Linux Server版本

mono "%EngineDir%\Engine\Binaries\DotNET\UnrealBuildTool.exe" MyGameServer Linux Development -Project="%GameDir%\MyGame.uproject"

构建安装版引擎

%EngineDir%\Engine\Build\BatchFiles\RunUAT.bat BuildGraph -target="Make Installed Build Win64" -script=Engine/Build/InstalledEngineBuild.xml -set:WithMac=false -set:WithAndroid=false -set:WithIOS=false -set:WithTVOS=false -set:WithLinux=false -set:WithHTML5=false -set:WithSwitch=false -WithDDC=false -set:WithWin32=false -set:WithLumin=false -set:WithPS4=false -set:WithXboxOne=false -set:WithHoloLens=false -set:GameConfigurations=Development

配置文件详见 : Engine/Build/InstalledEngineBuild.xml

构建好的安装版引擎在: %EngineDir%\LocalBuilds\Engine\Windows 目录中

注1: 可放置一个 %EngineDir%/Engine/Build/InstalledBuild.txt 空文件,将当前引擎标识成一个InstalledEngine

注2: InstalledEngine主要用来提升团队开发效率,让构建机生产好这些二进制内容,而不需要每个人都来编译一遍。

① 引擎编辑器(UE4Editor.exe)、构建工具链(UBT、UHT、UAT)以及第三方工具(UnrealPak.exe、ShaderCompileWorker.exe、UnrealLightmass.exe等)

(a)引擎编辑器、第三方工具和UHT     在%EngineDir%/Engine/Binaries/Win64目录中

(b)UBT、UAT     在%EngineDir%/Engine/Binaries/DotNET目录中

② 引擎及项目依赖的所有模块的.h、.generated.h、.lib、.dll文件

(a).h     在%EngineDir%/Engine/Source、%EngineDir%/Engine/Plugins/<xxx>/Source目录中

(b).generated.h、.lib     在%EngineDir%/Engine/Intermediate、%EngineDir%/Engine/Plugins/<xxx>/Intermediate目录中

(c).dll     在%EngineDir%/Engine/Binaries/Win64、%EngineDir%/Engine/Plugins/<xxx>/Binaries/Win64目录中

注3: cpp代码不是必须的

UnrealPak

解压未加密的MyGame-Android_ASTC.pak包到%Output%/MyGame-Android_ASTC目录中

%EngineDir%/Engine/Binaries/Win64/UnrealPak.exe "%MyGame-arm64-es2%/assets/MyGame/Content/Paks/MyGame-Android_ASTC.pak" -Extract "%Output%/MyGame-Android_ASTC"

有Crypto.json秘钥文件时,解压加密的MyGame-Android_ASTC.pak包到%Output%/MyGame-Android_ASTC目录中

%EngineDir%/Engine/Binaries/Win64/UnrealPak.exe "%MyGame-arm64-es2%/assets/MyGame/Content/Paks/MyGame-Android_ASTC.pak" -Extract "%Output%/MyGame-Android_ASTC" -cryptokeys="%GameDir%/Saved/Cooked/Android_ASTC/MyGame/Metadata/Crypto.json"

无Crypto.json秘钥文件时,解压加密的MyGame-Android_ASTC.pak包到%Output%/MyGame-Android_ASTC目录中

%EngineDir%/Engine/Binaries/Win64/UnrealPak.exe "%MyGame-arm64-es2%/assets/MyGame/Content/Paks/MyGame-Android_ASTC.pak" -Extract "%Output%/MyGame-Android_ASTC" -encryptindex -encryptionini -projectdir="%GameDir%" -enginedir="%EngineDir%" -Platform=Windows

注1: 该方式会通过读取%GameDir%/Config/DefaultCrypto.ini中的秘钥来解密

注2: 需要在项目的%GameDir%/Config/DefaultEngine.ini中进行如下配置,来将项目标识为一个加密类型

windows打包pak

"%EngineDir%\Engine\Binaries\Win64\UnrealPak.exe" %GameDir%\UAGame.uproject %GameDir%\Saved\StagedBuilds\WindowsNoEditor\UAGame\Content\Paks\pakchunk0-WindowsNoEditor.pak -create=%EngineDir%\Engine\Programs\AutomationTool\Saved\Logs\PakList_pakchunk0-WindowsNoEditor.txt -cryptokeys=%GameDir%\Saved\Cooked\WindowsNoEditor\UAGame\Metadata\Crypto.json -order=%GameDir%\Build\WindowsNoEditor\FileOpenOrder\CookerOpenOrder.log -encryptindex -sign -patchpaddingalign=2048 -platform=Windows  -multiprocess -abslog=%EngineDir%\Engine\Programs\AutomationTool\Saved\Logs\UnrealPak-pakchunk0-WindowsNoEditor-2022.02.15-16.33.18.txt 

Android打包pak

"%EngineDir%\Engine\Binaries\Win64\UnrealPak.exe" %GameDir%\UAGame.uproject %GameDir%\Saved\StagedBuilds\Android_ASTC\UAGame\Content\Paks\pakchunk1-Android_ASTC.pak -create=%EngineDir%\Eng
ine\Programs\AutomationTool\Saved\Logs\PakList_pakchunk1-Android_ASTC.txt -cryptokeys=%GameDir%\Saved\Cooked\Android_ASTC\UAGame\Metadata\Crypto.json -order=%GameDir%\Build\Android_ASTC\FileOpenOrder\CookerOpenOrder.log -encryptindex -platform=Android  -multiprocess -abslog=%EngineDir%\Engine\Programs\AutomationTool\Saved\Logs\U
nrealPak-pakchunk1-Android_ASTC-2022.02.15-13.05.20.txt

IOS打包pak

"$EngineDir$/Engine/Binaries/Win64/UnrealPak.exe" $GameDir$/UAGame.uproject $GameDir$/Saved/StagedBuilds/IOS/cookeddata/uagame/content/paks/pakchunk1-ios.pak -create="/Users/admin/L
ibrary/Logs/Unreal Engine/LocalBuildLogs/PakList_pakchunk1-ios.txt" -cryptokeys=$GameDir$/Saved/Cooked/IOS/UAGame/Metadata/Crypto.json -order=/Users/admin/UAGroup
/R6Game/Build/IOS/FileOpenOrder/CookerOpenOrder.log -encryptindex -platform=IOS -AlignForMemoryMapping=16384  -multiprocess -abslog="/Users/admin/Library/Logs/Unreal Engine/LocalBu
ildLogs/UnrealPak-pakchunk1-ios-2022.02.15-13.03.40.txt"

UnrealPak的log所在目录: %EngineDir%\Engine\Programs\UnrealPak\Saved\Logs\UnrealPak.log

UHT扫描头文件命令行

// UHT扫描UE4工程
"%EngineDir%\Engine\Binaries\Win64\UnrealHeaderTool.exe" UE4Editor "%EngineDir%\Engine\Intermediate\Build\Win64\UE4Editor\DebugGame\UE4Editor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -abslog="%EngineDir%\Engine\Programs\UnrealBuildTool\Log_UHT.txt" 
// UHT扫描MyGame项目工程
"%EngineDir%\Engine\Binaries\Win64\UnrealHeaderTool.exe" "%GameDir%\MyGame.uproject" "%GameDir%\Intermediate\Build\Win64\MyGameEditor\DebugGame\MyGameEditor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -abslog="%EngineDir%\Engine\Programs\UnrealBuildTool\Log_UHT.txt"
// UHT扫描ShaderCompileWorker工程
"%EngineDir%\Engine\Binaries\Win64\UnrealHeaderTool.exe" ShaderCompileWorker "%EngineDir%\Engine\Intermediate\Build\Win64\ShaderCompileWorker\Development\ShaderCompileWorker.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -abslog="%EngineDir%\Engine\Programs\UnrealBuildTool\Log_UHT.txt"

一些log

UnrealHeaderTool: UnrealEngine\Engine\Programs\UnrealHeaderTool\Saved\Logs\UnrealHeaderTool.log

UnrealBuildTool: UnrealEngine\Engine\Programs\UnrealBuildTool\Log.txt

AppData\Local\UnrealBuildTool\Log.txt (InstalledEngine)

UAT: UnrealEngine\Engine\Programs\AutomationTool\Saved\Logs\Log.txt

UnrealEngine\Engine\Programs\AutomationTool\Saved\Logs\Cook-2020.07.13-17.32.30.txt

MyGame: MyGame\Saved\Logs\MyGame.log

Mac osx上的log

UnrealHeaderTool: ~\Library\ Logs/Unreal Engine/ UnrealHeaderTool/ UnrealHeaderTool.log

UnrealBuildTool: UnrealEngine\Engine\Programs\UnrealBuildTool\Log.txt

UAT: ~\Library\ Logs/Unreal Engine/LocalBuildLogs 中,包含一下一些文件

Cook-2020.07.13-17.46.05.txt   // Cook资源的log
FinalCopy_NonUFSFiles.txt
FinalCopy_NonUFSFilesDebug.txt
FinalCopy_UFSFiles.txt
Log.txt    // 编译ios版本的log
Manifest_NonUFSFiles_IOS.txt
PakList_basicasset-ios.txt
PakList_uagame-ios.txt
PrePak_IOS_NonUFSFiles.txt
PrePak_IOS_NonUFSFilesDebug.txt
PrePak_IOS_UFSFiles.txt
UBT-UAGame-IOS-Development.txt
UBT-UnrealPak-Mac-Development.txt
UnrealPak-basicasset-ios-2020.07.13-17.47.12.txt
UnrealPak-uagame-ios-2020.07.13-17.47.12.txt

MyGame: ~\Library\ Logs/Unreal Engine/MyGameEditor/MyGame.log

(1)不要把DDC(Derived Data Cache)目录设置到机械硬盘上,要放在SSD上,否则使用编辑器时会很卡(如:移动场景中的物体) 注: 其缺省会在<User Folder>/AppData/Local/UnrealEngine中

(2)去掉UE4和游戏工程的所有Depends(工程右键菜单 -- Build Dependencies -- Project Dependencies ...),手动编译各个工程,防止修改一个工程后,引发依赖该工程发生重编译

(3)去掉UE4和游戏工程NMake命令行上的对ShaderCompileWorker的依赖

可在引擎工程:Engine/Saved/UnrealBuildTool/BuildConfiguration.xml中关闭 Generate Visual Studio Project files 时对ShaderCompileWorker的依赖

<?xml version="1.0" encoding="utf-8" ?>
 <Configuration xmlns="https://www.unrealengine.com/BuildConfiguration">
     <BuildConfiguration>
         <bEditorDependsOnShaderCompileWorker>false</bEditorDependsOnShaderCompileWorker>
     </BuildConfiguration>
 </Configuration>

(4)关闭每次构建项目都自动构建UnrealHeaderTool

在Engine/Saved/UnrealBuildTool/BuildConfiguration.xml中增加如下配置:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration xmlns="https://www.unrealengine.com/BuildConfiguration">
     <UEBuildConfiguration>
         <bDoNotBuildUHT>true</bDoNotBuildUHT>
     </UEBuildConfiguration>
</Configuration>

注1: 从UnrealHeaderTool.Build.cs中可知:UHT依赖6个基础的引擎模块: Core CoreUObject Json Projects TraceLog (Core、CoreUObject依赖TraceLog,所以UHT也间接依赖它)和 BuildSettings (Core依赖BuildSettings,所以UHT也间接依赖它)

注2: 开启UHT的DoNotBuild模式,会导致Unlua中Program类型的模块 UnLuaDefaultParamCollector UnLuaIntelliSense 编译不出来。

可将它们编译出来后,再开启UHT的DoNotBuild模式

(5)Tools - Options - Projects and Solutions,取消勾选Always show Error List if build finishes with errors,防止每次编译有错,vs都自动激活显示Error List面板

(6)关闭Visual Studio自带git插件,避免每次编译都要进行git status的检查,提升开发效率

1>------ Build started: Project: MyTest1, Configuration: Debug_Editor x64 ------
1>Using 'git status' to determine working set for adaptive non-unity build (G:\svn\UnrealEngine).
1>Using 'git status' to determine working set for adaptive non-unity build (G:\svn\MyTest1).
1>Waiting for 'git status' command to complete

(7)将Solution Configurations的宽度调成为150,让其能显示完全

菜单项:Tools - Options - Customize

(8)编译时不要用满所有cpu的核,否则计算机会非常卡,基本无法进行其他工作

在BuildConfiguration.xml中进行配置,将MaxProcessorCount设置成7(我本机是8核cpu)

<?xml version="1.0" encoding="utf-8" ?>
 <Configuration xmlns="https://www.unrealengine.com/BuildConfiguration">
     <ParallelExecutor>
         <MaxProcessorCount>7</MaxProcessorCount>
     </ParallelExecutor>
     <BuildConfiguration>
         <bAllowXGE>true</bAllowXGE>
     </BuildConfiguration>
 </Configuration>

注: bAllowXGE表示是否开启IncredBuild(XGE)

BuildConfiguration.xml会按照下面路径的顺序来进行查找来加载   更多信息详见: Build Configuration

① 引擎工程:Engine/Saved/UnrealBuildTool/BuildConfiguration.xml
② <User Folder>/AppData/Roaming/Unreal Engine/UnrealBuildTool/BuildConfiguration.xml
③ <My Documents>/Unreal Engine/UnrealBuildTool/BuildConfiguration.xml

还有一种快速禁掉IncredBuild方法是将 c:\Programe Files(x86)\IncredBuild\ xgConsole.exe 改名

(9)如果只是想禁用shader编译的IncredBuild的加速,可在Engine\Config\ConsoleVariables.ini中打开 r.XGEShaderCompile = 0

(10)从vs直接运行或调试运行UE时,不要进行任何编译行为

(11) UnrealVS扩展 中文

右键Visual Studio工具栏区域,弹出快捷菜单,勾选显示UnrealVS工具栏

安装文件在:Engine\Extras\UnrealVS

代码在:Engine\Source\Programs\UnrealVS

提供了如下功能:

a. 设置启动项目
b. 绑定构建启动项目的命令
c. 设置命令行参数
d. 批量构建项目
f. 快速构建项目菜单

(12)出现断点打不上(空心带黄色感叹号)的情况(如:调试Release版本、可执行程序目录发生过变化等),需要做一下检查和设置

① vs处于调试态时,在菜单Debug -- Windows -- Modules面板里,搜索目标模块的pdb是否加载

② vs设置:Tools - Options - Debugging  -  取消勾选“Require source files to exactly match the original version”

(13)改善C++调试:在vs进行C++调试时更好地查看Unreal自定义类型变量(如:FName,FString,TArray等)

运行Engine\Extras\VisualStudioDebugging\InstallVisualizers.bat(支持vs2012、vs2013、vs2015)进行安装

对于vs2017等更高版本,可以手动将Engine\Extras\VisualStudioDebugging\UE4.natvis拷贝到%USERPROFILE%\Documents\Visual Studio 2017\Visualizers目录中进行手动安装

安装完成后,重启vs

vs设置:Tools - Options - Debugging  -  取消勾选“Show raw structure of objects in variables windows”

(14)开启vs的Intellisense(智能感知)功能

在编写代码实时键入时,IntelliSense将重新编译C++,进行实时错误和筛选。此功能比VAX的语法检查更加强大,其使用的是完整C++编译器,可逐行验证代码,可极大加快工作流

注1: 左下角的图标表示图标IntelliSense编译程序正在运行

注2: vs默认是开启Intellisense的,如果没有启用,请检查以下设置项

(15)配置并使用ue4的Snippet自动完成,来插入模板代码

配置方法:拷贝Engine\Extras\VisualStudioSnippets中所有的snippet文件到C:\Users\<user>\Documents\Visual Studio 2017\Code Snippets\Visual C++\My Code Snippets目录中,并重启vs

然后在UE4的项目中输入: ue4 就会出现snippet列表,选择某项snippet后就会插入对应模板代码

扩展: ue4snippets github Snippet各个文件说明

(16)禁用“编译出错时始终显示错误列表”(Always show Error List if build finishes with error) 注: 防止编译失败后自动弹出错误列表

(17)Disable显示非活动代码块 注: 关闭该选项后,代码会正常显示(#if 为false的代码块会变灰)

更多请参考: 设置虚幻引擎的Visual Studio 中文

运行引擎和项目

启动引擎编辑器

"%EngineDir%/Engine/Binaries/Win64/UE4Editor.exe" -skipcompile

启动项目编辑器

"%EngineDir%/Engine/Binaries/Win64/UE4Editor.exe"  "%GameDir%\MyGame.uproject" -skipcompile

单机启动游戏单机

"%EngineDir%/Engine/Binaries/Win64/UE4Editor.exe"  "%GameDir%\MyGame.uproject" TestMap_Main -game -skipcompile

"%EngineDir%\Engine\Binaries\Win64\UE4Editor.exe"  "%GameDir%/MyGame.uproject" /Game/TestMap -game -PIEVIACONSOLE  -Multiprocess  GameUserSettingsINI="%GameDir%/Saved/Config/Windows/PIEGameUserSettings0.ini" -MultiprocessSaveConfig -MultiprocessOSS -messaging -SessionName="Play in Standalone Game" -windowed -WinX=309 -WinY=152 SAVEWINPOS=1 -ResX=1280 -ResY=720

启动本地ds

"%EngineDir%/Engine/Binaries/Win64/UE4Editor.exe"  "%GameDir%\MyGame.uproject" TestMap_Main -game -server -log -skipcompile

启动游戏并联网加入本地ds

"%EngineDir%/Engine/Binaries/Win64/UE4Editor.exe"  "%GameDir%\MyGame.uproject" 127.0.0.1 -game -skipcompile

① win64 -- Development版本包  会输出到%Win64Dir%\WindowsNoEditor目录中

%EngineDir%\Engine\Build\BatchFiles\RunUAT.bat -ScriptsForProject=%GameDir%\MyGame.uproject BuildCookRun -project=%GameDir%\MyGame.uproject -targetplatform=Win64 -clientconfig=Development -ue4exe=UE4Editor-Cmd.exe -noP4 -iterate -cook -pak -package -stage -archive -archivedirectory=%Win64Dir% -nocompileeditor -prereqs -nodebuginfo -build -CrashReporter -utf8output -compressed

打包后的目录结构如下:

启动器exe 将自身的命令行参数( UAGame -FeatureLevelES31 )传给并拉起 游戏exe

启动器exe pdb符号表 所在目录:D:\svn\UnrealEngine\Engine\Binaries\Win64\BootstrapPackagedGame-Win64-Shipping.pdb

main函数 所在代码文件:D:\svn\UnrealEngine\Engine\Source\Programs\Windows\BootstrapPackagedGame\Private\BootstrapPackagedGame.cpp

游戏exe pdb符号表 所在目录:D:\svn\R6Game\Binaries\Win64\UAGame-Win64-Test.pdb

main函数 所在代码文件:D:\svn\UnrealEngine\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp

② Android -- Debug版本包  会输出到%ApkDir%\Android_ETC2目录中 windows下安装Android构建环境 en

%EngineDir%\Engine\Build\BatchFiles\RunUAT.bat -ScriptsForProject=%GameDir%\MyGame.uproject BuildCookRun -nocompileeditor -nop4 -iterate -project=%GameDir%\MyGame.uproject -cook -stage -archive -archivedirectory=%ApkDir% -package -clientconfig=Debug -ue4exe=UE4Editor-Cmd.exe -compressed -pak -prereqs -nodebuginfo -targetplatform=Android -cookflavor=ETC2 -build -distribution -utf8output -compile

cook Android ASTC包命令:

%EngineDir%\Engine\Binaries\Win64\UE4Editor-Cmd.exe %GameDir%\MyGame.uproject -run=Cook  -TargetPlatform=Android_ASTC -fileopenlog -unversioned -abslog=%EngineDir%\Engine\Programs\AutomationTool\Saved\Cook-2020.12.08-09.48.23.txt -stdout -CrashForUAT -unattended -NoLogTimes  -UTF8Output

注: -iterate参数表示增量cook。在编辑器中打包不会带该参数,会执行全量cook

环境变量 -- XXX的用户变量:

ANDROID_HOME    E:\NVPACK\android-sdk-windows
ANDROID_NDK_ROOT    E:\NVPACK\android-ndk-r14b
ANT_HOME     E:\NVPACK\apache-ant-1.8.2
GRADLE_HOME    E:\NVPACK\gradle-2.9
JAVA_HOME    E:\NVPACK\jdk1.8.0_77
NDK_ROOT    E:\NVPACK\android-ndk-r14b
NDKROOT    E:\NVPACK\android-ndk-r14b
NVPACK_NDK_TOOL_VERSION    4.9
NVPACK_NDK_VERSION    android-ndk-r14b
NVPACK_ROOT    E:\\NVPACK
Path    E:\NVPACK\gradle-2.9\bin;E:\NVPACK\apache-ant-1.8.2\bin;E:\NVPACK\jdk1.8.0_77\bin;E:\NVPACK\android-ndk-r14b;E:\NVPACK\android-sdk-windows\extras\android\support;E:\NVPACK\android-sdk-windows\build-tools;E:\NVPACK\android-sdk-windows\platform-tools;E:\NVPACK\android-sdk-windows\tools;

注: 若想升级ndk,可从 这儿 下载,解压后放到E:\NVPACK目录中,并更新NDK_ROOT、NDKROOT及Path环境变量中的路径即可

在项目设置 -- Platforms -- Android页面中,点击Accept SDK License同意一下   注:会在E:\NVPACK\android-sdk-windows\licenses下生成一个 android-sdk-license 文件

C:\Users\<用户名>\.gradle目录、C:\Users\<用户名>\.android目录

配置gradle的代理(所在文件: C:\Users\<用户名>\.gradle\gradle.properties

systemProp.https.proxyPort=8080
systemProp.http.proxyHost=web-proxy.oa.com
systemProp.https.proxyHost=web-proxy.oa.com
systemProp.http.proxyPort=8080

③ iOS -- Debug版本包  会输出到${IpaDir}/IOS目录中 注: 设置DefaultGame.ini中[/Script/UnrealEd.ProjectPackagingSettings]标签下的BuildConfiguration= PPBC_Debug

${%EngineDir%}/Engine/Build/BatchFiles/RunUAT.sh -ScriptsForProject=${GameDir}/MyGame.uproject BuildCookRun -nocompileeditor -nop4 -project=${GameDir}/MyGame.uproject -cook -stage -archive -archivedirectory=${IpaDir} -package -clientconfig=Debug -ue4exe=UE4Editor -compressed -pak -prereqs -nodebuginfo -targetplatform=IOS -build -manifests -CrashReporter -utf8output -compile

注1: 在windows配置好iOS远程构建环境,也可以在windows上构建iOS版本  详见: 在 Windows 上构建 iOS 版本

注2: Mac上cook失败,提示:CookResults: Error: Package Native Shader Library failed for MacNoEditor.   可通过关掉Project Settings - Packing - Shared Material Native Library来解决

④ Linux ds -- Debug版本包  会输出到${LinuxDir}目录中

${%EngineDir%}/Engine/Build/BatchFiles/RunUAT.sh BuildCookRun -project=${GameDir}/MyGame.uproject -build -cook -pak -stage -archive -archivedirectory=${LinuxDir} -package -serverconfig=Debug -server -noclient -targetplatform=Linux -ue4exe=UE4Editor -prereqs -nop4 -utf8output

注1 :cook的临时文件存放在:%GameDir%\Saved\Cooked

注2 :最后的版本存放在:%GameDir%\Archive

清理Development版本的UE4引擎

"%EngineDir%\Engine\Build\BatchFiles\Clean.bat" UE4Editor Win64 Development -waitmutex

清理Development版本的MyGame项目

"%EngineDir%\Engine\Build\BatchFiles\Clean.bat" MyGameEditor Win64 Development "%GameDir%\MyGame.uproject" -waitmutex

为MyGame项目的TestMap_Main地图构建光照

"%EngineDir%\Engine\Build\BatchFiles\RunUAT.bat" RebuildLightmaps -project=%GameDir%\MyGame.uproject -MapsToRebuildLightMaps=TestMap_Main

在windows上重新打包和重签名,生成ipa文件  注:ipa文件生成在%GameDir%\Binaries\IOS\MyGame.ipa

"%EngineDir%\Engine\Binaries\DotNET\IOS\IPhonePackager.exe" RepackageFromStage "%GameDir%\MyGame.uproject" -config Development -schemename MyGame -schemeconfig "Development" -sign -codebased -stagedir "%GameDir%\Saved\StagedBuilds\IOS" -project "%GameDir%\MyGame.uproject" -provision "DGame.mobileprovision" -certificate "iPhone Developer: cert RDM (CFG5EWN5ME)"

找不到XINPUT1_3.dll

安装引擎目录下 Engine\Extras\Redist\en-us\UE4PrereqSetup_x64.exe

关于解决方案的Configuration

1. Debug 游戏模块和引擎模块均开启调试模式

2. DebugGame  游戏模块开启调试模式,引擎模板开启优化

3. Development  游戏模块和引擎模块均开启优化

4. Shipping 发行版本(会去掉所有编辑器功能、stat统计、性能分析以及GM命令等)游戏模块和引擎模块均开启优化

5. Test  含有stat统计、性能分析以及GM命令的Shipping版本

6. 带Editor表示为编辑器版本(含有很多编辑器相关的功能) 使用 MyGameEditor.Target.cs 的项目配置

7. 带Client表示为客户端版本    使用 MyGameClient.Target.cs 的项目配置

8. 带Server表示为服务器版本  使用 MyGameServer.Target.cs 的项目配置

9. 都不带表示为客户端+服务器版本    使用 MyGame.Target.cs 的项目配置

在windows下编译通过,而在其他Linux、Android、iOS等平台编译不过的一些情况:

1. 斜杠问题

如:#include "Misc \ FileHelper.h"   -->  #include "Misc / FileHelper.h"

2. 大小写问题

如:#include "HAL/PlatformFile M anager.h"  --> #include "HAL/PlatformFile m anager.h"

3. 非英文字符

如:UE_LOG(LogTemp, Warning, TEXT("test xx ");  --> UE_LOG(LogTemp, Warning, TEXT("test ( xx ) ");

4. 使用非UE4的类型

如: INT AmmoCount;   --> int32 AmmoCount;

5. 使用L来修饰字符串

如: L "MyTools"  --> TEXT ("MyTools")

6. 缺少#include某些头文件

原因是不同编译器递归展开头文件规则存在差别

Press Button, Drink Coffee : An Overview of UE4 build pipeline and maintenance【CEDEC 2019】