UE5运行时创建slate窗口

加入"Slate","SlateCore"模块

Actor.cpp

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"模块

cpp 复制代码
// 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;
};
cpp 复制代码
// 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);
}
相关推荐
uppp»24 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
m0_748256144 小时前
SpringBoot
java·spring boot·后端
阿华的代码王国4 小时前
【从0做项目】Java搜索引擎(3)
java·搜索引擎·项目
Mr.朱鹏4 小时前
针对Feign客户端请求体参数处理问题
java·jvm·spring boot·spring·spring cloud·maven·intellij-idea
新兴ICT项目支撑5 小时前
天翼云910B部署DeepSeek蒸馏70B LLaMA模型实践总结
linux·运维·服务器·910b·天翼云·deepseek r1
涛粒子6 小时前
Spring Bean 生命周期的执行流程
java·后端·spring
刘_sy7 小时前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
梦幻通灵7 小时前
IDEA通过Contince接入Deepseek
java·ide·intellij-idea
web150850966417 小时前
SQL 建表语句详解
java·数据库·sql
xiangxiongfly9157 小时前
Java 设计模式之迭代器模式
java·设计模式·迭代器模式