UE5 slate BlankProgram独立程序系列

源码版Engine\Source\Programs\中copy BlankProgram文件夹,重命名为ASlateLearning,修改所有文件命名及内部名称。

ASlateLearning.Target.cs

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.Collections.Generic;

[SupportedPlatforms(UnrealPlatformClass.All)]
public class ASlateLearningTarget : TargetRules
{
	public ASlateLearningTarget(TargetInfo Target) : base(Target)
	{
		Type = TargetType.Program;
		IncludeOrderVersion = EngineIncludeOrderVersion.Latest;
		LinkType = TargetLinkType.Monolithic;
		LaunchModuleName = "ASlateLearning";
	
		bBuildDeveloperTools = false;
		
		bool bDebugOrDevelopment = Target.Configuration == UnrealTargetConfiguration.Debug || Target.Configuration == UnrealTargetConfiguration.Development;
		//bBuildWithEditorOnlyData = Target.Platform.IsInGroup(UnrealPlatformGroup.Desktop) && bDebugOrDevelopment;
	
		bBuildWithEditorOnlyData = true;
		bCompileAgainstEngine = false;
		bCompileAgainstCoreUObject = true;
		bCompileAgainstApplicationCore = true;
		bCompileICU = false;
		
		bIsBuildingConsoleApplication = true;
	}
}

ASlateLearning.Build.cs

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class ASlateLearning : ModuleRules
{
	public ASlateLearning(ReadOnlyTargetRules Target) : base(Target)
	{
		PublicIncludePaths.Add("Runtime/Launch/Public");
		PrivateIncludePaths.Add("Runtime/Launch/Private");

		PrivateDependencyModuleNames.AddRange(
			new string[]
			{
				"AppFramework",
				"Core",
				"ApplicationCore",
				"Projects",
				"Slate",
				"SlateCore",
				"StandaloneRenderer",
			});
	}
}

ASlateLearning.cpp

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#include "ASlateLearning.h"

#include "RequiredProgramMainCPPInclude.h"
#include "StandaloneRenderer.h"

DEFINE_LOG_CATEGORY_STATIC(LogASlateLearning, Log, All);

IMPLEMENT_APPLICATION(LogASlateLearning, "ASlateLearning");

INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
{
	GEngineLoop.PreInit(ArgC, ArgV);

	FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
	FSlateApplication::InitHighDPI(true);

	const TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(800, 600));

	FSlateApplication::Get().AddWindow(MainWindow.ToSharedRef());

	while (!IsEngineExitRequested())
	{
		BeginExitIfRequested();
		FSlateApplication::Get().PumpMessages();
		FSlateApplication::Get().Tick();
	}

	FEngineLoop::AppExit();

	return 0;
}

运行后会出现创建的窗口,后面继续扩展

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#include "ASlateLearning.h"

#include "RequiredProgramMainCPPInclude.h"
#include "StandaloneRenderer.h"

DEFINE_LOG_CATEGORY_STATIC(LogASlateLearning, Log, All);

IMPLEMENT_APPLICATION(LogASlateLearning, "ASlateLearning");

INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
{
	GEngineLoop.PreInit(ArgC, ArgV);

	FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
	FSlateApplication::InitHighDPI(true);

	const TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(800, 600))
		[
			SNew(SHorizontalBox) + SHorizontalBox::Slot()
			[
				SNew(SButton).Text(NSLOCTEXT("L10N", "Key", "Button Content"))
			]
		];

	FSlateApplication::Get().AddWindow(MainWindow.ToSharedRef());
	

	while (!IsEngineExitRequested())
	{
		BeginExitIfRequested();
		FSlateApplication::Get().PumpMessages();
		FSlateApplication::Get().Tick();
	}

	FEngineLoop::AppExit();

	return 0;
}

运行后窗口样式,button太大因为Slot默认的对其策略是HAlign::Fill,VAlign::Fill,调整后的这部分代码应该是

使用SOverlay::FOverlaySlot* Slot;个Slot的指针,用.Expose(Slot)获取指针,Expose方法填入一个插槽指针并将其返回,此后,我们通过APi可以获得一个SWidget的引用,此时的代码应该是这样了

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#include "ASlateLearning.h"

#include "RequiredProgramMainCPPInclude.h"
#include "StandaloneRenderer.h"

DEFINE_LOG_CATEGORY_STATIC(LogASlateLearning, Log, All);

IMPLEMENT_APPLICATION(LogASlateLearning, "ASlateLearning");

INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
{
	GEngineLoop.PreInit(ArgC, ArgV);

	FSlateApplication::InitializeAsStandaloneApplication(GetStandardStandaloneRenderer());
	FSlateApplication::InitHighDPI(true);

	SOverlay::FOverlaySlot* Slot;

	const TSharedPtr<SWindow> MainWindow = SNew(SWindow).ClientSize(FVector2D(800, 600))
		[
			SNew(SOverlay)
			+ SOverlay::Slot().HAlign(HAlign_Left).VAlign(VAlign_Top).Expose(Slot)
			[
				SNew(SHorizontalBox)
				+ SHorizontalBox::Slot().HAlign(HAlign_Left).VAlign(VAlign_Top)
				[
					SNew(SButton).Text(NSLOCTEXT("L10N", "Key", "Button Content"))
				]
			]
		];

	SWidget& slotwidget = Slot->GetWidget().Get();
	SHorizontalBox& HorizontalBox = static_cast<SHorizontalBox&>(slotwidget);

	for (int i = 0; i <5;++i)
	{
		HorizontalBox.AddSlot()
		[
			SNew(SHorizontalBox)
				+ SHorizontalBox::Slot().HAlign(HAlign_Left).VAlign(VAlign_Top)
				[
					SNew(SButton).Text(NSLOCTEXT("L10N", "Key", "Button Content"))
				]
		];
	}
	
	FSlateApplication::Get().AddWindow(MainWindow.ToSharedRef());


	while (!IsEngineExitRequested())
	{
		BeginExitIfRequested();
		FSlateApplication::Get().PumpMessages();
		FSlateApplication::Get().Tick();
	}

	FEngineLoop::AppExit();

	return 0;
}

运行后的窗口变成了

可以为每个Button添加一些方法,通过OnClicked_Lambda之类的方法

相关推荐
枫の准大一22 分钟前
C++从零到满绩——类和对象(中)
开发语言·c++
HEX9CF28 分钟前
【数字图像处理+MATLAB】通过 Roberts, Prewitt, Sobel, LoG 等算子实现图像边缘检测:使用 edge 函数
开发语言·matlab·edge
凡人的AI工具箱39 分钟前
40分钟学 Go 语言高并发实战:高性能缓存组件开发
开发语言·后端·缓存·架构·golang
大白的编程日记.42 分钟前
【C++笔记】数据结构进阶之二叉搜索树(BSTree)
开发语言·数据结构·c++·笔记
每天一个秃顶小技巧44 分钟前
01.Golang 源码目录结构
开发语言·后端·golang
小柯J桑_1 小时前
C++:用红黑树封装map与set-1
开发语言·c++·set·map·红黑树
2401_853275731 小时前
Java IO 基础知识总结下
java·开发语言
时光の尘1 小时前
C语言菜鸟入门·关键字·union的用法
运维·服务器·c语言·开发语言·c·printf
我想睡到自然醒₍˄·͈༝·͈˄*₎◞ ̑1 小时前
【Android】View的解析—滑动篇
android·java
诸神黄昏EX1 小时前
Android 常用命令和工具解析之内存相关
android·java·开发语言