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);
}
相关推荐
咖啡咖_CoffCa23 分钟前
【UE5】新建Editor Standalone Window插件,之前(或当前)创建插件的按钮消失(被顶掉/占用)的问题
ue5·虚幻引擎·插件开发
wangwangmoon_light38 分钟前
0.0 编码基础模板
java·数据结构·算法
未来之窗软件服务1 小时前
操作系统应用开发(二十三)RustDesk ng反向代理—东方仙盟筑基期
服务器·远程桌面·仙盟创梦ide·东方仙盟·rustdek
Terio_my1 小时前
Spring Boot 热部署配置与自定义排除项
java·spring boot·后端
JAVA学习通2 小时前
微服务项目->在线oj系统(Java-Spring)--C端用户(超详细)
java·开发语言·spring
计算机毕业设计小帅2 小时前
【2026计算机毕业设计】基于jsp的毕业论文管理系统
java·开发语言·毕业设计·课程设计
xxxxxxllllllshi3 小时前
Java 代理模式深度解析:从静态到动态,从原理到实战
java·开发语言·笔记·算法·代理模式
冷yan~3 小时前
Spring AI与智能代理模式的深度解析
java·spring·ai·ai编程
天航星3 小时前
Docker 安装 Jenkins
java·运维·jenkins