简单介绍一下GenerateProjectFiles.bat以及如何生成UnrealBuildTool.exe

先构建 MsBuild 所需环境配置
然后利用 MsBuild 编译UnrealBuildTool
最后调用…\Binaries\DotNET\UnrealBuildTool.exe -ProjectFiles %* ,即调用UnrealBuildTool.exe并传入所需参数
运行UnrealBuildTool.exe以生成Visual Studio解决方案和项目文件

1、源码中最外层的下图bat,如果按照官方教程一步步走下来,windows平台上会进入下一个bat
在这里插入图片描述
2、第二个bat如下图
在这里插入图片描述
3、几个关键点, UnrealBuildToolFiles.txt 和 UnrealBuildToolPrevFiles.txt 都是在上述bat中生成的
在这里插入图片描述

UnrealBuildToolFiles.txt 是扫描下面文件夹下所有的.CS文件生成的一个文件路径信息

UnrealBuildToolPrevFiles.txt 可以理解成是上一次点击生成的文件路径信息

两个文件形成对比,检查UBT目录中的文件是否已更改。
在这里插入图片描述

GenerateProjectFiles.bat内每句话的注释

@echo off 
rem @echo off 关闭回显,不显示正在执行的批处理命令及执行的结果
rem rem 注释
rem ## Unreal Engine 4 Visual Studio project setup script 
rem 虚幻4 Visual Studio 工程的启动脚本
rem 版权
rem ## This script is expecting to exist in the UE4 root directory.  It will not work correctly
rem ## if you copy it to a different location and run it.
rem 这个脚本要放在UE4的根目录中。如果你将他复制到一个另一个位置,将无法运行
setlocal
rem 使启动批处理文件中环境变量的本地化
echo Setting up Unreal Engine 4 project files...
rem echo XXXXX 打印XXXXX到输出面板
rem ## First, make sure the batch file exists in the folder we expect it to.  This is necessary in order to
rem ## verify that our relative path to the /Engine/Source directory is correct
rem 首先,确保这个批处理文件处于正确的位置,这是为了确保我们对于 /Engine/Source 路径的相对路径是正确的
if not exist "%~dp0..\..\Source" goto Error_BatchFileInWrongLocation
rem 如果不存在 "",直接运行到Error_BatchFileInWrongLocation, 其中 "%~dp0" 代表的是当前批处理文件所在完整目录, 所以指代"UnrealEngine\Engine\Source"该路径
rem ## Change the CWD to /Engine/Source.  We always need to run UnrealBuildTool from /Engine/Source!
rem 改变脚本的运行路径到 /Engine/Source 我们需要UnrealBuildTool在 /Engine/Source 路径下运行 CWD是指脚本运行路径
pushd "%~dp0..\..\Source"
rem 保存当前目录,并切换当前目录为 "%~dp0..\..\Source"
if not exist ..\Build\BatchFiles\GenerateProjectFiles.bat goto Error_BatchFileInWrongLocation
rem 如果 "UnrealEngine\Engine\Build\BatchFiles\GenerateProjectFiles.bat" 不存在 运行到Error_BatchFileInWrongLocation
rem ## Check to make sure that we have a Binaries directory with at least one dependency that we know that UnrealBuildTool will need
rem ## in order to run.  It's possible the user acquired source but did not download and unpack the other prerequiste binaries.
rem 检查以确保我们有一个Binaries目录,其中至少有一个依赖项,我们知道UnrelbuildTool将需要该依赖项才能运行。用户可能获得了源代码,但没有下载和解包其他必备的二进制文件。
if not exist ..\Build\BinaryPrerequisitesMarker.dat goto Error_MissingBinaryPrerequisites
rem 如果 "UnrealEngine\Engine\Build\BinaryPrerequisitesMarker.dat" 不存在 运行Error_MissingBinaryPrerequisites
rem ## Get the path to MSBuild
rem 获取MSBuild的路径
call "%~dp0GetMSBuildPath.bat"
rem 调用 "UnrealEngine\Engine\Build\BatchFiles\dp0GetMSBuildPath.bat" 该批处理文件
if errorlevel 1 goto Error_NoVisualStudioEnvironment
rem 如果错误等级≥1 运行Error_NoVisualStudioEnvironment    注意:if errorlevel 的比较方式是“大于或等于”。若是返回值大于或等于指定的数字,则条件成立,运行命令。因此返回值必须按照从大到小的顺序排列
rem ## If we're using VS2017, check that NuGet package manager is installed. MSBuild fails to compile C# projects from the command line with a cryptic error if it's not: 
rem ## https://developercommunity.visualstudio.com/content/problem/137779/the-getreferencenearesttargetframeworktask-task-wa.html
rem 如果使用的VS2017版本 需要检查NuGet已经安装。MSBuild无法从命令行编译C#项目,如果不是:https://developercommunity.visualstudio.com/content/problem/137779/the-getreferencenearesttargetframeworktask-task-wa.html
if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" goto NoVsWhere
rem 如果不存在 "C:\Program Files(x86)\Microsoft Visual Studio\Installer\vswhere.exe"  运行NoVsWhere
set MSBUILD_15_EXE=
for /f "delims=" %%i in ('"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath') do (
	if exist "%%i\MSBuild\15.0\Bin\MSBuild.exe" (
		set MSBUILD_15_EXE="%%i\MSBuild\15.0\Bin\MSBuild.exe"
		goto FoundMsBuild15
rem for /d、/f、/l 对目录、文件、字符串、序列等进行循环遍历操作 参数 delims=设置分隔符  
rem 设置 MSBUILD_15_EXE "C:\Program Files x86\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe"  如果该路径存在
:FoundMsBuild15
set MSBUILD_15_EXE_WITH_NUGET=
for /f "delims=" %%i in ('"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere" -latest -products * -requires Microsoft.Component.MSBuild -requires Microsoft.VisualStudio.Component.NuGet -property installationPath') do (
	if exist "%%i\MSBuild\15.0\Bin\MSBuild.exe" (
		set MSBUILD_15_EXE_WITH_NUGET="%%i\MSBuild\15.0\Bin\MSBuild.exe"
		goto FoundMsBuild15WithNuget
rem 设置 MSBUILD_15_EXE_WITH_NUGET "C:\Program Files x86\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe 如果该路径存在
:FoundMsBuild15WithNuget
if not [%MSBUILD_EXE%] == [%MSBUILD_15_EXE%] goto NoVsWhere
if not [%MSBUILD_EXE%] == [%MSBUILD_15_EXE_WITH_NUGET%] goto Error_RequireNugetPackageManager
:NoVsWhere
rem Check to see if the files in the UBT directory have changed. We conditionally include platform files from the .csproj file, but MSBuild doesn't recognize the dependency when new files are added. 
rem 检查UBT目录中的文件是否已更改。我们有条件地包含.csproj文件中的平台文件,但添加新文件时,MSBuild无法识别依赖关系
md ..\Intermediate\Build >nul 2>nul
rem md命令是用来创建文件夹 >nul 是屏蔽操作成功显示的信息,但是出错还是会显示 2>nul 是屏蔽操作失败显示的信息,如果成功依旧显示
dir /s /b Programs\UnrealBuildTool\*.cs >..\Intermediate\Build\UnrealBuildToolFiles.txt
rem dir /s /b 获得文件夹及其子文件夹下的所有文件和子文件夹 所有后缀以cs结尾的文件输入到 UnrealBuildToolFiles.txt
if not exist ..\Platforms goto NoPlatforms
for /d %%D in (..\Platforms\*) do (
	if exist %%D\Source\Programs\UnrealBuildTool dir /s /b %%D\Source\Programs\UnrealBuildTool\*.cs >> ..\Intermediate\Build\UnrealBuildToolFiles.txt
rem ..\Platforms 文件夹存在的话 就把 ..\Platforms\*\Source\Programs\UnrealBuildTool 下的cs文件路径保存到 UnrealBuildToolFiles.txt
:NoPlatforms
if not exist ..\Restricted goto NoRestricted
for /d %%D in (..\Restricted\*) do (
	if exist %%D\Source\Programs\UnrealBuildTool dir /s /b %%D\Source\Programs\UnrealBuildTool\*.cs >> ..\Intermediate\Build\UnrealBuildToolFiles.txt
rem ..\Restricted 文件夹存在的话 就把 ..\Restricted\*\Source\Programs\UnrealBuildTool 下的cs文件路径保存到 UnrealBuildToolFiles.txt
:NoRestricted
fc /b ..\Intermediate\Build\UnrealBuildToolFiles.txt ..\Intermediate\Build\UnrealBuildToolPrevFiles.txt >nul 2>nul
rem fc /b 以二进制文件的形式比较  ..\Intermediate\Build\UnrealBuildToolFiles.txt ..\Intermediate\Build\UnrealBuildToolPrevFiles.txt
if not errorlevel 1 goto SkipClean
rem 如果 errorlevel ≥ 1 运行 SkipClean
copy /y ..\Intermediate\Build\UnrealBuildToolFiles.txt ..\Intermediate\Build\UnrealBuildToolPrevFiles.txt >nul
rem 复制..\Intermediate\Build\UnrealBuildToolFiles.txt 覆盖 ..\Intermediate\Build\UnrealBuildToolPrevFiles.txt   /y 不使用确认是否要覆盖现有目标文件的提示
%MSBUILD_EXE% /nologo /verbosity:quiet Programs\UnrealBuildTool\UnrealBuildTool.csproj /property:Configuration=Development /property:Platform=AnyCPU /target:Clean
rem MSBuild指令 /nologo 隐藏启动版权标志和版权消息  清理 Programs\UnrealBuildTool\UnrealBuildTool.csproj 并设置里面的两个参数Configuration=Development Platform=AnyCPU 
:SkipClean
%MSBUILD_EXE% /nologo /verbosity:quiet Programs\UnrealBuildTool\UnrealBuildTool.csproj /property:Configuration=Development /property:Platform=AnyCPU /target:Build
rem MSBuild指令 /nologo 隐藏启动版权标志和版权消息  编译构造 Programs\UnrealBuildTool\UnrealBuildTool.csproj 并设置里面的两个参数Configuration=Development Platform=AnyCPU 
if errorlevel 1 goto Error_UBTCompileFailed
rem ## Run UnrealBuildTool to generate Visual Studio solution and project files
rem ## NOTE: We also pass along any arguments to the GenerateProjectFiles.bat here
..\Binaries\DotNET\UnrealBuildTool.exe -ProjectFiles %*
rem 运行UnrealBuildTool.exe 接受 -ProjectFiles %* 和所有传入的参数
if errorlevel 1 goto Error_ProjectGenerationFailed
rem ## Success!
exit /B 0
:Error_BatchFileInWrongLocation
echo.
echo GenerateProjectFiles ERROR: The batch file does not appear to be located in the /Engine/Build/BatchFiles directory.  This script must be run from within that directory.
echo.
pause
goto Exit
:Error_MissingBinaryPrerequisites
echo.
echo GenerateProjectFiles ERROR: It looks like you're missing some files that are required in order to generate projects.  Please check that you've downloaded and unpacked the engine source code, binaries, content and third-party dependencies before running this script.
echo.
pause
goto Exit
:Error_NoVisualStudioEnvironment
echo.
echo GenerateProjectFiles ERROR: Unable to find a valid installation of Visual Studio.  Please check that you have Visual Studio 2017 or Visual Studio 2019 installed, and the MSBuild component is selected as part of your installation.
echo.
pause
goto Exit
:Error_RequireNugetPackageManager
echo.
echo UE4 requires the NuGet Package Manager to be installed to use %MSBUILD_EXE%. Please run the Visual Studio Installer and add it from the individual components list (in the 'Code Tools' category).
echo.
pause
goto Exit
:Error_UBTCompileFailed
echo.
echo GenerateProjectFiles ERROR: UnrealBuildTool failed to compile.
echo.
pause
goto Exit
:Error_ProjectGenerationFailed
echo.
echo GenerateProjectFiles ERROR: UnrealBuildTool was unable to generate project files.
echo.
pause
goto Exit
:Exit
rem ## Restore original CWD in case we change it
exit /B 1

PS 如果看不懂bat内东西可以去查一下批处理指令以及参考MSBuild参考
关于UnrealBuildTool的理解

原因和新版本的vs2017有关,博主通过vs 安装器更新到最新版本的vs2017,缺少支持上述文件的管理包。 解决办法是打开Visual Studio Installer,选择修改,在单个组件中勾上“NuGet 包管理器”组件。安装完成之后在再运行该文件就不会提... UnrealVS 在 VS2015--Tools--Extension 中看不到了。 IDE里也看不到,不能使用 重新安装UnrealVS,提示:"The extension i... Running D:/my_program/Epic Games/UE4.21/UE_4.21/Engine/Binaries/DotNET/UnrealBuildTool.exe -projectfiles -project="C:/Users/Administrator/Documents/Unreal Projects/***/****.uproject" -game -rocket -progress 使用UE4创建C++项目,提示上面错误信息。 解决方法: 如果你尝试了以下几种方法: 1. 实际是调用了Engine\Build\BatchFiles\GenerateProjectFiles.bat2. 然后调用Engine\Build\BatchFiles\GetMSBuildPath.bat3. 依次查找2017,2015,2013 vs的MSBuild.exe,首先找到哪个版本的MSBuild.exe就直接成功退出4. 调用MSBuild.exe生成UnrealBuildT... C:\Program Files\Epic Games\UE_4.14\Engine\Binaries\Win64 下面找一下UnrealVersionSelector文件,,复制到C:\Program Files\Epic Games\UE_4.14\Engine\Binaries\Win64 并且双击运行即可 很多小伙伴在做开发的时候都用Epic管理的UE引擎,可是在企业实际开发中,需要用到虚幻的源代码。UE和Unity相比的优势也是开源,所以使用引擎的源码很有必要,便于了解底层和修改底层逻辑很有帮助。很多小伙伴不清楚如何编译,本文就详细讲解一下如何编译安装拿到手的源码。 1.更新依赖 • 打开引擎文件目录运行Setup.bat 在列出缺失文件后输入y开始安装 2.完成后,运行GenerateProjectFiles.bat • 之后编译UE4源码,打开UE4.sln。编译文件,时间很长、耐心等待。注意工具栏“