Unreal Engine calls local file picker (FileDialog)

Preface

When developing some desktop applications, you will need to use the engine to pick up the file content of the local disk. In conventional software, when you need to pick up disk content, you will wake up the file picker on the system desktop to complete it. How to wake up the desktop content picker in Unreal Engine? The operation is actually very simple.

Unreal Engine wakes up the disk file picker

Operation

  1. Import modules

"DesktopPlatform", "Slate", "SlateCore" are dependent modules and need to be imported.

1PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HTTP" });
2PrivateDependencyModuleNames.AddRange(new string[] { "DesktopPlatform", "Slate", "SlateCore" });
  1. Execute the code to open the window
 1//Get the window handle
 2void* NativeWinHandle = GEngine->GameViewport->GetWindow()->GetNativeWindow()->GetOSWindowHandle();
 3if (NativeWinHandle)
 4{
 5	//Get the desktop platform module object
 6	IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
 7	if (DesktopPlatform)
 8	{
 9		//Used to store the picked asset name in the array
10		TArray<FString> OutNames;
11		//Open the window
12		DesktopPlatform->OpenFileDialog(NativeWinHandle, TEXT("Open folder"), "d:\\", FString(""), TEXT("Image file|*.png"), 0, OutNames);
13	}
14}

Header file

1#include "DesktopPlatformModule.h"

OpenFileDialog function parameter description:

  • Parent node window handle
  • File picker window title
  • Picker default path
  • Picker default lock file
  • Picker file type constraint (If left blank, no constraint on type)
  • Single file pick or multiple file pick, 0 single file pick, 1 multiple file pick
  • After the pick is completed, the array where the picked content (path) is stored

Description

About pick type constraint

If you want to add type constraints, add them in the format of "description|type". Here are some examples:

  • Pick only png type, "Image File|*.png" (refer to the screenshot above)
  • Pick png type and jpg type, you can use semicolon separation, "Image File|*.png;*.jpg”
  • Need to pick up image type, text type, json type, "Image File|*.png;*.jpg|Txt File|*.txt|Json File|*.json"

About call blocking

After the file picker is opened, the calling thread will be blocked. When the user selects the file, it will continue to execute.

Unreal Engine version: 4.27.2

Translations:

Comment