UE4 打开系统的文件选择窗口|打开windows文件选取窗口
UE4中打开文件窗口等桌面平台支持功能的接口类 IDesktopPlatform
IDesktopPlatform::OpenFileDialog(const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray<FString>& OutFilenames, int32& outFilterIndex )
virtual bool OpenFileDialog(const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray<FString>& OutFilenames, int32& outFilterIndex ) = 0;
/**
* Opens the "save file" dialog for the platform
*
* @param ParentWindowHandle The native handle to the parent window for this dialog
* @param DialogTitle The text for the title of the dialog window
* @param DefaultPath The path where the file dialog will open initially
* @param DefaultFile The file that the dialog will select initially
* @param Flags Details about the dialog. See EFileDialogFlags.
* @param FileTypes The type filters to show in the dialog. This string should be a "|" delimited list of (Description|Extensionlist) pairs. Extensionlists are ";" delimited.
* @param OutFilenames The filenames that were selected in the dialog
* @return true if files were successfully selected
*/
UE4中已包含在Windows、Linux、MacOs下对IDesktopPlatform的实现
使用时只需要从FDesktopPlatformModule模块获取当前桌面操作系统的IDesktopPlatform具体对象
IDesktopPlatform* = FDesktopPlatformModule::Get();
使用示例:
#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h"
#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h"
TArray<FString> path; //选中文件路径
FString fileType = TEXT("XmlFile (*.xml)|*.xml"); //过滤文件类型
FString defaultPath = FPaths::ConvertRelativePathToFull(FPaths::ProjectDir()); //文件选择窗口默认开启路径
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
bool bSuccess = DesktopPlatform->OpenFileDialog(nullptr, TEXT("XmlImportDialog"), defaultPath, TEXT(""), *fileType, EFileDialogFlags::None, path);
if (bSuccess)
{
//文件选择成功,文件路径 path
}