当前位置: 首页 > article >正文

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


http://www.kler.cn/a/399021.html

相关文章:

  • C++网络编程之SSL/TLS加密通信
  • 【jvm】HotSpot中方法区的演进
  • 现代密码学|公钥密码体制 | RSA加密算法及其数学基础
  • React中组件通信的几种方式
  • 微服务即时通讯系统的实现(客户端)----(2)
  • Python3.11.9+selenium,选择证书用多线程+键盘enter解决
  • iOS UI自动化 Appium的元素定位方式及比较
  • matlab-fmincon函数做优化、optimoptions用法
  • 千图网 AI 绘画平台——智能图像创作工具
  • Ubuntu杀死指定进程
  • Linux:进程的优先级 进程切换
  • python 2小时学会八股文-数据结构
  • Spring MVC初探
  • 基于YOLOv8深度学习的公共卫生防护口罩佩戴检测系统(PyQt5界面+数据集+训练代码)
  • npm install执行一直在转圈
  • 如何使用正则表达式验证域名
  • 校园交友系统的设计与实现(开源版+三端交付+搭建+售后)
  • 选择租用网站服务器的适用范围是什么?
  • 【python】Bokeh 与 Plotly:创建交互式数据可视化工具
  • Xcode控制台“po“错误:表达式解析失败
  • 笔记|M芯片MAC (arm64) docker上使用 export / import / commit 构建amd64镜像
  • 软考之面向服务架构SOA
  • 跨平台WPF框架Avalonia教程 十三
  • redis7.x源码分析:(3) dict字典
  • 山寨一个Catch2的SECTION
  • python strip() 详解