Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to start a FilePicker in separate thread/Coroutine? #134

Open
ClmnsRck opened this issue Sep 26, 2023 · 1 comment
Open

How to start a FilePicker in separate thread/Coroutine? #134

ClmnsRck opened this issue Sep 26, 2023 · 1 comment

Comments

@ClmnsRck
Copy link

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?

@TobiasWehrum
Copy link

TobiasWehrum commented Feb 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants