一、效果
二、实现
UE4 中的消息类型如下:
namespace EAppMsgType
{
/**
* Enumerates supported message dialog button types.
*/
enum Type
{
Ok,
YesNo,
OkCancel,
YesNoCancel,
CancelRetryContinue,
YesNoYesAllNoAll,
YesNoYesAllNoAllCancel,
YesNoYesAll,
};
}
不同类型对应了消息框最后不同的按钮组合(命名上就能看出有哪些按钮了👍),所有组合如下:
Ok | YesNo |
---|---|
OkCancel | YesNoCancel |
CancelRetryContinue | YesNoYesAllNoAll |
YesNoYesAllNoAllCancel | YesNoYesAll |
默认情况下,点击 任何按钮 以及右上角的叉,消息框都会自动关闭,不同按钮的自定义功能,由代码控制。
打开消息框的接口是:
/**
* Open a modal message box dialog
* @param MessageType Controls buttons dialog should have
* @param Message Text of message to show
* @param OptTitle Optional title to use (defaults to "Message")
*/
static EAppReturnType::Type Open( EAppMsgType::Type MessageType, const FText& Message, const FText* OptTitle = nullptr);
使用方法如下:
FString Output = "1. Missing AnimMontages: ";
for (int i = 0; i < 3; ++i)
{
Output += "\n\t\tblablabla";
}
FText const Title = FText::FromString("Your Own Title");
FText const DialogText = FText::Format(LOCTEXT("Output", "{0}"), FText::FromString(Output));
EAppReturnType::Type const ReturnType = FMessageDialog::Open(EAppMsgType::OkCancel, DialogText, &Title);
返回值类型用于判断点击的是哪个按钮(返回值类型 EAppReturnType
和 EAppMsgType
的对应关系详见 FMessageDialog::Open
,即上边那个函数)。
if (ReturnType == EAppReturnType::Type::Ok)
{
// When clicked Ok button
}
else if (ReturnType == EAppReturnType::Type::Cancel)
{
// When clicked Cancel button
}
三、注意事项
FText::Format(LOCTEXT("Output", "{0}"), FText::FromString(Output));
中的第一个参数是用作 Key,填什么貌似没什么表现上的区别。Open
的第三个参数是消息框的标题(const FText*
类型),默认是Message
。