UE5运行时创建slate窗口

加入"Slate","SlateCore"模块

Actor.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyWindowClass.h"


// Sets default values
AMyWindowClass::AMyWindowClass()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AMyWindowClass::BeginPlay()
{
	Super::BeginPlay();

	// Create a new SWindow
	TSharedRef<SWindow> NewWindow = SNew(SWindow)
		.AutoCenter(EAutoCenter::PreferredWorkArea)
		.SupportsMaximize(false)
		.SupportsMinimize(false)
		.SizingRule(ESizingRule::Autosized)
		.CreateTitleBar(true)
		[
			// Add content to the window (for example, a vertical box with text)
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
			.HAlign(HAlign_Center)
			.VAlign(VAlign_Fill)
			[
				SNew(STextBlock)
				.Text(FText::FromString("Hello, this is a new window!"))
			]
		];

	// Add the window to the Slate application
	FSlateApplication::Get().AddWindow(NewWindow);
}

// Called every frame
void AMyWindowClass::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

运行时创建scolorpicer窗口

加入"AppFramework","Slate","SlateCore"模块

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActorClass.generated.h"

UCLASS()
class MYCOLORPICERT_API AMyActorClass : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AMyActorClass();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	TSharedPtr<SWindow> ColorPickerWindow;
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActorClass.h"

#include "Widgets/Colors/SColorPicker.h"

// Sets default values
AMyActorClass::AMyActorClass()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AMyActorClass::BeginPlay()
{
	Super::BeginPlay();
	
	// 创建一个新的 Slate 窗口
	ColorPickerWindow = SNew(SWindow)
		.Title(FText::FromString("Color Picker"))
		//.ClientSize(FVector2D(400, 300)) // 设置窗口尺寸
		.IsTopmostWindow(true) // 保持在最前面
		.SizingRule(ESizingRule::Autosized)
		[
			SNew(SColorPicker)
		];
	// 将窗口添加到 UI 层
	FSlateApplication::Get().AddWindow(ColorPickerWindow.ToSharedRef());

}

void AMyActorClass::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);
	
	if (ColorPickerWindow)
	{
		FSlateApplication::Get().RequestDestroyWindow(ColorPickerWindow.ToSharedRef());
	}
}

// Called every frame
void AMyActorClass::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我救我自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值