Unreal Engine 5 C++: 插件编写03 | MessageDialog

在虚幻引擎编辑器中编写Warning弹窗

准备工作

FMessageDialog These functions open a message dialog and display the specified informations there.

EAppReturnType::TypeUnreal Engine 中用于表示应用程序对话框(如消息对话框)返回结果的枚举类型。具体来说,当你使用 FMessageDialog::Open 或类似函数显示一个对话框时,用户的选择(例如点击"确定"、"取消"、"是"、"否"等按钮)会以 EAppReturnType::Type 的形式返回。这使得开发者能够根据用户的选择执行相应的逻辑。

EAppReturnType 是一个枚举类,其定义可能类似于以下内容:

cpp 复制代码
namespace EAppReturnType
{
    enum Type
    {
        No,
        Yes,
        YesAll,
        NoAll,
        Cancel,
        Ok,
        Retry,
        Continue
    };
}

示例代码1

cpp 复制代码
if (NumOfDuplicates<=0) {

	//Print(TEXT("Pls enter a valid number"), FColor::Red);
	FText MsgTitle = FText::FromString(TEXT("Warning"));

	FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT("Please enter a valid number")), &MsgTitle);
	return;
}

插件效果1

插件效果2

------------------------------编辑器右下角slate notification的效果------------------------------------

A list of non-intrusive messages about the status of currently active work.

non-intrusive messages

非侵入性的消息,意味着这些消息不会干扰用户的操作或体验,通常是轻量级的提示或通知。

准备工作:需要的header file

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Slate/Framework/Notifications/FSlateNotificationManager?application_version=4.27

A class which manages a group of notification windows

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Slate/Widgets/Notifications/SNotificationList?application_version=4.27

A list of non-intrusive messages about the status of currently active work.

示例代码2

cpp 复制代码
#pragma once
#include "Misc/MessageDialog.h"
#include "Widgets/Notifications/SNotificationList.h"
#include "Framework/Notifications/NotificationManager.h"

void Print(const FString& Message, const FColor& Color)
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 8.f, Color, Message);
	}
}

void PrintLog(const FString& Message)
{
	UE_LOG(LogTemp, Warning, TEXT("%s"),*Message);
}

EAppReturnType::Type ShowMsgDialog(EAppMsgType::Type MsgType, const FString& Message, 
bool bShowMsgAsWarning = true) 
{
	if (bShowMsgAsWarning == true) 
	{
		FText MsgTitle = FText::FromString(TEXT("Warning"));

		return FMessageDialog::Open(MsgType, FText::FromString(Message), &MsgTitle);
	//return FMessageDialog::Open(MsgType, FText::FromString(TEXT("Warning: ") + Message));
	}

	else
	{
		return FMessageDialog::Open(MsgType, FText::FromString(Message));
	}
}

void ShowNotifyInfo(const FString& Message)
{
	FNotificationInfo NotifyInfo(FText::FromString(Message));
	NotifyInfo.bUseLargeFont = true;
	NotifyInfo.FadeOutDuration = 7.f;

	FSlateNotificationManager::Get().AddNotification(NotifyInfo);
}
cpp 复制代码
	if (Counter > 0)
	{
		ShowNotifyInfo(TEXT("Successfully duplicated" + FString::FromInt(Counter) + "files"));
		//Print(TEXT("Successfully duplicated" + FString::FromInt(Counter) + "files"), FColor::Green);
	}
相关推荐
肆忆_19 分钟前
# cilly-vm-cpp 重构复盘(第 1 阶段:SRP)
c++
天若有情67344 分钟前
循环条件隐藏陷阱:我发现了「同循环双条件竞态问题」
c++·学习·算法·编程范式·while循环··竞态
是梦终空1161 小时前
C++中的职责链模式变体
开发语言·c++·算法
mjhcsp1 小时前
C++遗传算法(Genetic Algorithm,GA):进化式全局优化的核心解析
开发语言·c++
仰泳的熊猫1 小时前
题目2270:蓝桥杯2016年第七届真题-四平方和
c++·算法·蓝桥杯
Aaswk2 小时前
蓝桥杯2025年第十六届省赛真题(更新中)
c语言·数据结构·c++·算法·职场和发展·蓝桥杯
王老师青少年编程3 小时前
信奥赛C++提高组csp-s之数论基础专题课:欧拉函数和欧拉定理2(编程案例实践)
c++·数论·欧拉函数·信奥赛·欧拉定理·csp-s·提高组
总斯霖3 小时前
P15445永远在一起!题解(月赛T2)
数据结构·c++·算法·深度优先
Yupureki4 小时前
《C++实战项目-高并发内存池》4.CentralCache构造
c语言·开发语言·c++·单例模式·github
样例过了就是过了4 小时前
LeetCode热题100 全排列
数据结构·c++·算法·leetcode·dfs