Create A Standalone Application in UE4

虽然UE是个游戏引擎,但并不是只能写游戏——你甚至可以用来写Win32 GUI程序😏.
通常,我们使用Editor创建一个UE的游戏项目,然后在其基础上构建自己的类并在游戏中使用。但是如果不想要创建一个游戏项目,UE也支持可以独立运行的程序(Standalone Application),能够从main函数来自主构建自己的程序,完全控制启用哪些Modules,而不依赖于引擎本身的逻辑架构,也可以将其作为学习和测试UE模块的轻便方法。

:UE并没有提供直接创建Standalone Application的方法,我自己写了一个创建Program项目的工具:hxhb/ue4program,并实现了一个独立运行工具的Demo:hxhb/UE4Launcher

Forward

本文的内容实践需要使用本地从源码构建出来的引擎(源码版引擎)而非从EpicGameLauncher安装的引擎(安装版引擎),我之前的文章里介绍过UBT是检测是否存在Engine/Build/InstalledBuild.txt该文件,来判断引擎是从Launcher安装的还是从源码编译的,但是源码版和安装版并不是只有这么多区别,一些ThridParty的build.cs是不一样的,会造成源码版和安装版引擎对于相同的代码会有不同的编译行为(比如Engine/Source/ThirdParty/HarfBuzz),虽然这些问题也可是可以搞定的,但是我不建议你那么做。

Create Program

首先,下载我写的hxhb/ue4program,将其添加到系统的PATH路径中。添加之后的用法如下:

1
2
# ue4program.exe ProgramName
$ ue4program.exe StandaloneApplication

此时会在当前目录下创建出一个StandaloneApplication文件夹,其目录结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
StandaloneApplication
│ GenerateProgramProject.bat
│ OpenProgramProject.bat
│ StandaloneApplication.Build.cs
│ StandaloneApplication.Target.cs

├─Resources
│ Icon.ico
│ Resource.h
│ Resource.rc
│ VersionResource.inl

└─Source
├─Private
│ │ RealExecutionMain.cpp
│ │ StandaloneApplication.cpp
│ │
│ ├─Console
│ │ ConsoleMain.cpp
│ │
│ └─Windows
│ WindowsMain.cpp

└─Public
RealExecutionMain.h
StandaloneApplication.h
StandaloneApplicationLog.h

本篇文章着重需要分析的是*.target.cs*.build.cs这两个文件.
创建出来之后,需要将StandaloneApplication文件夹移动到Engine\Source\Programs下,UE所有的辅助程序(UHT/UBT等)都在这个目录之下。
然后,运行GenerateProgramProject.bat,这是我仿照UE的GenerateProjectFiles.bat写的一个脚本,通过调用UBT来创建Standalone Application程序,命令如下:

1
2
# bash path in Engine\Source\Programs\StandaloneApplication
$ UnrealBuildTool.exe -notinstallengine -ProjectFiles StandaloneApplication

注意:直接在安装版引擎中使用上面的命令会报错(不允许创建非Game项目),因为虽然传入了-notinstallengine参数,但是对Engine/Build/InstalledBuild.txt文件检测的优先级高于-notinstallengine

1
2
3
4
Zhalipeng MINGW64 /d/UE_4.18/Engine/Source/Programs/StandaloneApplication (master)
$ ../../../../Engine/Binaries/DotNET/UnrealBuildTool.exe -notinstallengine -ProjectFiles StandaloneApplication
ERROR: UnrealBuildTool Exception: A game project path was not specified, which is required when generating project files using an installed build or passing -game on the command line

我写的脚本中做了检测,如果使用的是安装版引擎会先重命名InstalledBuild.txt,生产完毕后会恢复(但是我还是建议不要在安装版引擎中执行)。其执行完毕后会在Engine\Intermediate\ProjectFiles下生成:

1
2
3
StandaloneApplication.vcxproj
StandaloneApplication.vcxproj.filters
StandaloneApplication.vcxproj.user

其实就是将当前项目添加到UE的解决方案,执行完毕之后,打开引擎根目录下的UE4.sln,就可以看到创建的项目了:

先来编译运行看一下结果:

Program *.target.cs

UBT支持数种Target类型(通过枚举类型TargetType来指定):

  • Game:独立运行的游戏;
  • Client:与Game相同,但不包含任何服务器代码;
  • Server:与Game相同,但不包含任何客户端代码;
  • Editor:UE编辑器的扩展;
  • Program:独立运行的程序;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Engine/Source/Programs/UnrealBuildTool/Configuration/TargetRules.cs
public enum TargetType
{
// Cooked monolithic game executable (GameName.exe). Also used for a game-agnostic engine executable (UE4Game.exe or RocketGame.exe)
Game,
// Uncooked modular editor executable and DLLs (UE4Editor.exe, UE4Editor*.dll, GameName*.dll)
Editor,
// Cooked monolithic game client executable (GameNameClient.exe, but no server code)
Client,
// Cooked monolithic game server executable (GameNameServer.exe, but no client code)
Server,
// Program (standalone program, e.g. ShaderCompileWorker.exe, can be modular or monolithic depending on the program)
Program,
}

关于.target.cs的文档介绍:UnrealBuildTool/Targets.

hxhb/ue4program创建生成的*.target.cs模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using UnrealBuildTool;
using System.Collections.Generic;

[SupportedPlatforms(UnrealPlatformClass.All)]
public class StandaloneApplicationTarget : TargetRules
{
public StandaloneApplicationTarget(TargetInfo Target) : base(Target)
{
Type = TargetType.Program;
LinkType = TargetLinkType.Monolithic;
LaunchModuleName = "StandaloneApplication";
ExtraModuleNames.Add("EditorStyle");
}

public override void SetupGlobalEnvironment(
TargetInfo Target,
ref LinkEnvironmentConfiguration OutLinkEnvironmentConfiguration,
ref CPPEnvironmentConfiguration OutCPPEnvironmentConfiguration
)
{
// Lean and mean
bCompileLeanAndMeanUE = true;

// No editor or editor-only data is needed
bBuildEditor = false;

// Whether to compile WITH_EDITORONLY_DATA disabled. Only Windows will use this, other platforms force this to false.
//bBuildWithEditorOnlyData = false;

// Compile out references from Core to the rest of the engine
bCompileAgainstEngine = false;

// Enabled for all builds that include the CoreUObject project. Disabled only when building standalone apps that only link with Core.
bCompileAgainstCoreUObject = true;

// Whether to include plugin support.
bCompileWithPluginSupport = true;

// Enable exceptions for all modules
bForceEnableExceptions = false;

// Enable RTTI for all modules.
// bForceEnableRTTI = true;

// If ture the program entrance is WinMain,otherwise entrance is main
bIsBuildingConsoleApplication = false;
}
}

目前,最需要关注的参数只有三个:

  • Type:需要指定为需要构建的目标类型TargetType.Program.
  • LinkType:指定为TargetLinkType.Monolithic则将目标编译成一个单个的可执行文件(不依赖任何DLL).
  • bIsBuildingConsoleApplicationture支持Console,函数入口为main;false则不支持Console,主函数入口为WinMain.

其他参数的(RTTI/Exceptions)均能在用到时根据需求从文档查询。

Program *.Build.cs

Program的*.Build.cs与普通的游戏项目的没有区别(因为Game是一个模块,Program也是),唯一的区别就是将这些模块构建成什么样的目标类型(由*.target.cs决定)。

Demo: UELauncher

hxhb/UE4Launcher是我使用UE的Program模式写的一个便捷的UE Project启动器,可以方便地加入启动参数,选择引擎版本,可以从hxhb/UE4Launcher/release下载并安装。我有一篇独立的文章来介绍该工具、更新日志与下载链接:开源一个虚幻引擎启动器UE Launcher

主界面:

编辑配置文件:

.uproject文件的右键菜单关联:

.uejson文件的右键菜单关联:

命令行的参数支持:

  • -g-g参数后跟.uproject文件,为.uproject生成.uejson配置文件;
  • -e-e参数后跟.uejson文件,打开窗口以编辑该配置文件;
  • -c-c参数后跟.uejson文件,启动该配置(不会打开编辑窗口)
1
2
3
4
5
6
# generate LauncherConf_ShooterGame.uejson
$ UE4Launcher.exe -g ShooterGame.uproject
# Launch Edit Window with .uejson
$ UE4Launcher.exe -e LauncherConf_ShooterGame.uejson
# Launch Conf
$ UE4Launcher.exe -c LauncherConf_ShooterGame.uejson

源码中也支持非常简单的扩展UE的其他工具,比如UBT/AutomationTool等,都可以增加为启动的工具(Editor/Editor-cmd为默认)。

End

使用Slate写UI可以参考SlateViewer这个模板示例的各种控件用法,也可以借鉴UnrealFrontend的实现。
当然Standalone Application能做的不仅仅是写UI这么简单,UE里的模块都可以用,是超级强大的Library,而且还是跨平台的,更多的奇淫巧技慢慢发掘吧。

全文完,若有不足之处请评论指正。

微信扫描二维码,关注我的公众号。

本文标题:Create A Standalone Application in UE4
文章作者:查利鹏
发布时间:2019年03月30日 18时14分
更新时间:2020年03月18日 14时10分
本文字数:本文一共有2.4k字
原始链接:https://imzlp.com/posts/31962/
许可协议: CC BY-NC-SA 4.0
文章禁止全文转载,摘要转发请保留原文链接及作者信息,谢谢!
您的捐赠将鼓励我继续创作!