UE4 Slate创建独立窗口APP

UE4 Slate创建独立窗口APP

UE4 可以用Slate模块创建 GUI窗体应用程序。

生成源码解决方案

假设源码目录:D:\Code\UnrealEngine-4.26\
分别运行Setup.batGenerateProjectFiles.bat生成UE4.sln解决方案。
在这里插入图片描述

创建Program

D:\Code\UnrealEngine-4.26\Engine\Source\Programs创建名为SlateAppProgramprogram
在这里插入图片描述
在这里插入图片描述

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)
			]
		];

在这里插入图片描述

原文链接

https://blog.csdn.net/mrbaolong/article/details/114439645

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值