在个人的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
注:如果是UE4.22版本,需要安装.net framework 4.6.2,安装完后在控制面板卸载列表中会有如下软件列表:
执行GenerateProjectFiles.bat生成UE4相关工程,生成的vs工程文件在Engine\Intermediate\ProjectFiles目录中
双击UE4.sln,开始编译相关工具和引擎 // 编译的中间文件生成在EngineSource\Engine\Intermediate\Build目录中
编译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
编译引擎工具
"%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
使用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
生成了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的vs项目工程文件 // 工程文件会生成到"%GameDir%\Intermediate\ProjectFiles"目录中
"%EngineDir%\Engine\Binaries\Win64\UnrealVersionSelector.exe" /projectfiles "%GameDir%\MyGame.uproject" //windows下 "%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工程
编译项目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下
编译单个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"
编译项目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目录中
一些log
UnrealHeaderTool:UnrealEngine\Engine\Programs\UnrealHeaderTool\Saved\Logs\UnrealHeaderTool.log
UnrealBuildTool:UnrealEngine\Engine\Programs\UnrealBuildTool\Log.txt
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)去掉UE4和游戏工程的所有Depends(工程右键菜单 -- Build Dependencies -- Project Dependencies ...),手动编译各个工程,防止修改一个工程后,引发依赖该工程发生重编译
(2)Tools - Options - Projects and Solutions,取消勾选Always show Error List if build finishes with errors,防止每次编译有错,vs都自动激活显示Error List面板
(3)将Solution Configurations的宽度调成为150,让其能显示完全
菜单项:Tools - Options - Customize
(4)编译时不要用满所有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
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改名
(5)如果只是想禁用shader编译的IncredBuild的加速,可在Engine\Config\ConsoleVariables.ini中打开r.XGEShaderCompile = 0
(6)从vs直接运行或调试运行UE时,不要进行任何编译行为
(7)UnrealVS扩展(中文)
右键Visual Studio工具栏区域,弹出快捷菜单,勾选显示UnrealVS工具栏
安装文件在:Engine\Extras\UnrealVS
代码在:Engine\Source\Programs\UnrealVS
提供了如下功能:
a. 设置启动项目
b. 绑定构建启动项目的命令
c. 设置命令行参数
d. 批量构建项目
f. 快速构建项目菜单
(8)出现断点打不上(空心带黄色感叹号)的情况(如:调试Release版本、可执行程序目录发生过变化等),需要做一下检查和设置
① vs处于调试态时,在菜单Debug -- Windows -- Modules面板里,搜索目标模块的pdb是否加载
② vs设置:Tools - Options - Debugging - 取消勾选“Require source files to exactly match the original version”
(9)改善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”
(10)开启vs的Intellisense(智能感知)功能
在编写代码实时键入时,IntelliSense将重新编译C++,进行实时错误和筛选。此功能比VAX的语法检查更加强大,其使用的是完整C++编译器,可逐行验证代码,可极大加快工作流
注1:左下角的图标表示图标IntelliSense编译程序正在运行
注2:vs默认是开启Intellisense的,如果没有启用,请检查以下设置项
(11)配置并使用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各个文件说明
更多请参考:设置虚幻引擎的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
启动本地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
② 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
环境变量 -- 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
注:在windows配置好iOS远程构建环境,也可以在windows上构建iOS版本 详见:在 Windows 上构建 iOS 版本
④ 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. 都不带表示为客户端版本 使用MyGame.Target.cs的项目配置
8. 带Server表示为服务器版本 使用MyGameServer.Target.cs的项目配置
在windows下编译通过,而在其他Linux、Android、iOS等平台编译不过的一些情况:
1. 斜杠问题
如:#include "Misc\FileHelper.h" --> #include "Misc/FileHelper.h"
2. 大小写问题
如:#include "HAL/PlatformFileManager.h" --> #include "HAL/PlatformFilemanager.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某些头文件
原因是不同编译器递归展开头文件规则存在差别