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
Unreal Engine wakes up the disk file picker

Operation

  1. Import modules

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

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

Header file

c++
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