UnityStandaloneFileBrowser icon indicating copy to clipboard operation
UnityStandaloneFileBrowser copied to clipboard

How to start a FilePicker in separate thread/Coroutine?

Open ClmnsRck opened this issue 2 years ago • 1 comments

Opening the file picker panel halts the entire unity application, even if started in Coroutine or while using OpenFilePickerAsync().

Is it possible to open a instance that is decoupled from the rest of the application?

ClmnsRck avatar Sep 26 '23 12:09 ClmnsRck

This apparently only happens in Windows. Unless you want to directly change code inside UnityStandaloneFileBrowser, you have treat calls differently on Windows.

Here is my solution for it:

void ShowDialog(string title)
{
#if PLATFORM_STANDALONE_WIN
	Task.Run(() => StandaloneFileBrowser.OpenFilePanel(title, "", "", false)).ContinueWithOnMainThread(OnCallbackTask);
#else
	StandaloneFileBrowser.OpenFilePanelAsync(title, "", "", false, OnCallback);
#endif
}

private void OnCallbackTask(Task<string[]> task)
{
	if (task.Exception != null)
	{
		// Some kind of exception happened in OpenFilePanel, somehow. Treat it however you see fit here.
		OnCallback([]);
	}
	else
	{
		OnCallback(task.Result);
	}
}

private void OnCallback(string[] paths)
{
	// Our usual callback
}

Note that it seems that the Unity app cannot close while the file dialog is still open - it will just hang if you press Alt+F4 until the dialog is closed. I haven't found a solution for that yet.

TobiasWehrum avatar Feb 22 '24 17:02 TobiasWehrum