UE4 Slate创建独立窗口APP
UE4
可以用Slate
模块创建 GUI
窗体应用程序。
生成源码解决方案
假设源码目录:D:\Code\UnrealEngine-4.26\
分别运行Setup.bat
和GenerateProjectFiles.bat
生成UE4.sln
解决方案。
创建Program
在D:\Code\UnrealEngine-4.26\Engine\Source\Programs
创建名为SlateAppProgram
的program
。
StateAppProgram.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
StateAppProgram.cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "SlateAppProgram.h"
#include "RequiredProgramMainCPPInclude.h"
#include "Framework/Application/SlateApplication.h"
#include "StandaloneRenderer.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/SWindow.h"
#include "Widgets/Images/SImage.h"
DEFINE_LOG_CATEGORY_STATIC(LogSlateAppProgram, Log, All);
/*
DECLARE_LOG_CATEGORY_EXTERN (LogMySlate, Log, All );
DEFINE_LOG_CATEGORY (LogMySlate)
*/
IMPLEMENT_APPLICATION(SlateAppProgram, "SlateAppProgram");
//INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
//{
// GEngineLoop.PreInit(ArgC, ArgV);
// UE_LOG(LogSlateAppProgram, Display, TEXT("Hello World"));
// return 0;
//}
int WINAPI WinMain(_In_ HINSTANCE hInInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR, _In_ int nCmdShow)
{
GEngineLoop.PreInit(GetCommandLineW());
FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
const FSlateBrush* icon = FCoreStyle::Get().GetBrush(TEXT("TrashCan"));
// 建立窗口
TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(1280, 720))
[
SNew(SImage).Image(icon)// 这里以Image做示范
];
FSlateApplication::Get().AddWindow(MainWindow.ToSharedRef());
//UE_LOG(LogSlateAppProgram, Display, TEXT("Hello World"));
// 消息循环
while (!IsEngineExitRequested())
{
FSlateApplication::Get().Tick();
FSlateApplication::Get().PumpMessages();
}
FSlateApplication::Shutdown();
return 0;
}
SlateAppProgram.Build.cs
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class SlateAppProgram : ModuleRules
{
public SlateAppProgram(ReadOnlyTargetRules Target) : base(Target)
{
PublicIncludePaths.Add("Runtime/Launch/Public");
PrivateIncludePaths.Add("Runtime/Launch/Private"); // For LaunchEngineLoop.cpp include
PrivateDependencyModuleNames.Add("Core");
PrivateDependencyModuleNames.Add("Projects");
//添加Slate依赖
PrivateDependencyModuleNames.AddRange(
new string[]
{
"Slate",
"SlateCore",
"StandaloneRenderer"
});
}
}
SlateAppProgram.Target.cs
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
using System.Collections.Generic;
[SupportedPlatforms(UnrealPlatformClass.Desktop)]
public class SlateAppProgramTarget : TargetRules
{
public SlateAppProgramTarget(TargetInfo Target) : base(Target)
{
Type = TargetType.Program;// 类型为Program
LinkType = TargetLinkType.Monolithic;// 模块的链接方式,Monolithic:1个,Modular:多个dll
LaunchModuleName = "SlateAppProgram";// 启动模块
// Lean and mean
//其还会决定bCompileLeanAndMeanUE
bBuildDeveloperTools = false;
// Never use malloc profiling in Unreal Header Tool. We set this because often UHT is compiled right before the engine
// automatically by Unreal Build Tool, but if bUseMallocProfiler is defined, UHT can operate incorrectly.
// 是否启用内存分析
bUseMallocProfiler = false;
// Editor-only data, however, is needed
bBuildWithEditorOnlyData = true;
// Currently this app is not linking against the engine, so we'll compile out references from Core to the rest of the engine
bCompileAgainstEngine = false;// 是否链接所有引擎程序生成的项目
//bCompileAgainstCoreUObject = false;
bCompileAgainstCoreUObject = true;// 需要连接到CoreUObject模块
//bCompileAgainstApplicationCore = false;
bCompileAgainstApplicationCore = true;
// UnrealHeaderTool is a console application, not a Windows app (sets entry point to main(), instead of WinMain())
//bIsBuildingConsoleApplication = true;
bIsBuildingConsoleApplication = false;// true:控制台app;false:窗体app
}
}
重新生成源码解决方案
运行GenerateProjectFiles.bat
生成UE4.sln
解决方案。
打开UE4.sln
,设置启动项目为SlateAppProgram
,编译并运行。
运行结果。
控制图标位置:
FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
const FSlateBrush* icon = FCoreStyle::Get().GetBrush(TEXT("TrashCan"));
// 建立窗口
TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(1280, 720))
[
SNew(SOverlay)
+SOverlay::Slot()
.HAlign(HAlign_Left)
.VAlign(VAlign_Top)
[
SNew(SImage).Image(icon)
]
];