游戏开发过程中,有时需要明显的提示游戏运行时出现的问题,比如配置错误等信息,不仅需要输出错误到日记文件,还需要明显的显示错误信息给到非程序人员看到。那么打开系统窗口提示就是一个非常好的选择。
案例1:
在UE4中,你可以使用 FMessageDialog::Open
函数来打开一个自定义的消息对话框。以下是一个简单的示例:
#include "Runtime/Engine/Public/Slate/Dialogs/MessageDialog.h"
void AMyTestActor::TestFMessageDialog()
{
FText Title = FText::FromString("My Dialog");
FText Message = FText::FromString("This is a message.The UE invokes the interface and displays the Windows window.Display this information.");
EAppMsgType::Type Type = EAppMsgType::OkCancel;
const EAppReturnType::Type Response = FMessageDialog::Open(Type, Message, &Title);
if (Response == EAppReturnType::Ok)
{
UE_LOG(LogTemp, Log, TEXT("User clicked OK."));
// 处理用户确认...
}
else if (Response == EAppReturnType::Cancel)
{
UE_LOG(LogTemp, Log, TEXT("User clicked Cancel."));
// 处理用户取消...
}
}
在这个示例中,我们使用了 FMessageDialog::Open
函数来打开一个自定义的消息对话框。Title
参数指定了对话框显示的标题文本,Message
参数指定了对话框显示的消息文本。Type
参数指定了对话框按钮类型。
如果用户单击了对话框上的“确定”按钮,则 FMessageDialog::Open
函数返回 EAppReturnType::Ok
。否则,如果用户单击了“取消”按钮,则函数返回 EAppReturnType::Cancel
。你可以使用这个返回值来处理用户的选择。
效果:
案例2:
在UE4中,你可以使用 FPlatformMisc::MessageBoxExt
函数来打开系统消息对话框。以下是一个简单的示例:
#include "Runtime/Core/Public/GenericPlatform/GenericPlatformMisc.h"
void AMyTestActor::TestMessageBoxExt()
{
FText Message = FText::FromString("This is a message.");
FText Title = FText::FromString("Message Box");
EAppMsgType::Type Type = EAppMsgType::Ok;
EAppReturnType::Type ret = FPlatformMisc::MessageBoxExt(Type, *Message.ToString(), *Title.ToString());
switch (ret)
{
case EAppReturnType::Ok:
UE_LOG(LogTemp, Log, TEXT("User clicked OK."));
// 处理用户确认...
break;
case EAppReturnType::Cancel:
UE_LOG(LogTemp, Log, TEXT("User clicked Cancel."));
// 处理用户取消...
break;
}
}
在这个示例中,我们使用了 FPlatformMisc::MessageBoxExt
函数来打开一个简单的消息对话框。Message
参数指定了对话框显示的消息文本,Title
参数指定了对话框的标题。Type
参数指定了对话框按钮类型。
如果用户单击了对话框上的“确定”按钮,则 FPlatformMisc::MessageBoxExt
函数返回 true。否则,它将返回 false。你可以使用这个返回值来处理用户的选择。
效果:
参考:
UE引擎代码:ProjectManager.cpp
bool FProjectManager::LoadModulesForProject( const ELoadingPhase::Type LoadingPhase )
{
DECLARE_SCOPE_CYCLE_COUNTER(TEXT("Loading Game Modules"), STAT_GameModule, STATGROUP_LoadTime);
bool bSuccess = true;
if ( CurrentProject.IsValid() )
{
TMap<FName, EModuleLoadResult> ModuleLoadFailures;
FModuleDescriptor::LoadModulesForPhase(LoadingPhase, CurrentProject->Modules, ModuleLoadFailures);
if ( ModuleLoadFailures.Num() > 0 )
{
FText FailureMessage;
for ( auto FailureIt = ModuleLoadFailures.CreateConstIterator(); FailureIt; ++FailureIt )
{
const EModuleLoadResult FailureReason = FailureIt.Value();
if( FailureReason != EModuleLoadResult::Success )
{
const FText TextModuleName = FText::FromName(FailureIt.Key());
if ( FailureReason == EModuleLoadResult::FileNotFound )
{
FailureMessage = FText::Format( LOCTEXT("PrimaryGameModuleNotFound", "The game module '{0}' could not be found. Please ensure that this module exists and that it is compiled."), TextModuleName );
}
else if ( FailureReason == EModuleLoadResult::FileIncompatible )
{
FailureMessage = FText::Format( LOCTEXT("PrimaryGameModuleIncompatible", "The game module '{0}' does not appear to be up to date. This may happen after updating the engine. Please recompile this module and try again."), TextModuleName );
}
else if ( FailureReason == EModuleLoadResult::FailedToInitialize )
{
FailureMessage = FText::Format( LOCTEXT("PrimaryGameModuleFailedToInitialize", "The game module '{0}' could not be successfully initialized after it was loaded."), TextModuleName );
}
else if ( FailureReason == EModuleLoadResult::CouldNotBeLoadedByOS )
{
FailureMessage = FText::Format( LOCTEXT("PrimaryGameModuleCouldntBeLoaded", "The game module '{0}' could not be loaded. There may be an operating system error, the module may not be properly set up, or a plugin which has been included into the build has not been turned on."), TextModuleName );
}
else
{
ensure(0); // If this goes off, the error handling code should be updated for the new enum values!
FailureMessage = FText::Format( LOCTEXT("PrimaryGameModuleGenericLoadFailure", "The game module '{0}' failed to load for an unspecified reason. Please report this error."), TextModuleName );
}
// Just report the first error
break;
}
}
FMessageDialog::Open(EAppMsgType::Ok, FailureMessage);
bSuccess = false;
}
}
OnLoadingPhaseCompleteEvent.Broadcast(LoadingPhase, bSuccess);
return bSuccess;
}
----