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);
}
相关推荐
William.csj2 小时前
服务器——“查询不到显卡驱动,且输入nvidia-smi报错”的解决办法
运维·服务器
麦兜*4 小时前
Spring Boot集成方案 + Elasticsearch向量检索,语义搜索核弹
java·spring boot·python·spring·elasticsearch·spring cloud·系统架构
Absinthe_苦艾酒4 小时前
JVM学习专题(四)对象创建过程
java·jvm·后端
路由侠内网穿透4 小时前
本地部署 SQLite 数据库管理工具 SQLite Browser ( Web ) 并实现外部访问
运维·服务器·开发语言·前端·数据库·sqlite
程序员奈斯5 小时前
苍穹外卖Day10
java
CodeHackerBhx5 小时前
Jenkins
java·运维·jenkins
忘忧人生5 小时前
docker 容器常用命令
java·docker·容器
橘颂TA5 小时前
【Linux】特效爆满的Vim的配置方法 and make/Makefile原理
linux·运维·服务器·vim
AI大法师6 小时前
企业级Linux服务器安全:防火墙规则配置与Web/SSH服务优化指南
linux·服务器·安全
慕y2746 小时前
Java学习第一百一十部分——CI/CD
java·学习·ci/cd