I am developing an application for the HoloLens 2 with Unity. I need some help understanding how to communicate and jump between the UWP environment and the .NET/Unity environment.
I am trying to use the FileOpenPicker with the PickSingleFileAsync() function after a button press to get the path of a selected file. Like in this Post i have kind of this scenario:
Code:
Part1: A method calls StartPicker() to get a file name with which subsequent calculations happen. But this method waits for the result => await StartPicker()
public async Task<String> StartPicker()
{
#if !UNITY_EDITOR && UNITY_WSA_10_0
Debug.Log("HOLOLENS 2 PICKER");
await FilePicker_Hololens();
#endif
#if UNITY_EDITOR
Debug.Log("UNITY_STANDALONE PICKER");
await FilePicker_Win();
#endif
//Wait for Picker retrieve path
// Note: Currently returns empty string as calling method has Task seems to be finished
return filePath;
}
Part 2: Method which should indicate if FileDialog is finished and therefore path is obtained. (LoadDataset() takes care of the actual loading if the path is available and should be waited for here too).)
#if !UNITY_EDITOR && UNITY_WSA_10_0
private async Task FilePicker_Hololens()
{
// Calls to UWP must be made on the UI thread.
UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
{
var filepicker = new FileOpenPicker();
filepicker.FileTypeFilter.Add("*");
filepicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
var file = await filepicker.PickSingleFileAsync();
//Note: Called after the file picker is closed
UnityEngine.WSA.Application.InvokeOnAppThread(async () =>
{
filePath = (file != null) ? file.Path : "Nothing selected";
await LoadDataset();
}, true);
}, true);
// Here we should be able to return a finished Task
}
#endif
Problem:
My problem is now that it seems FilePicker_Hololens() returns a finished Task even if the FileDialog is not finished, hence no path is acquired. I await the execution StartPicker() as well as the execution of FilePicker_Hololens inside. Nevertheless, the caller of StartPicker() executes its remaining method synchronously and does not leave the method at await. I think my Problems lies in the jumping between UWP and Unity Context with UnityEngine.WSA.Application.InvokeOnUIThread
and UnityEngine.WSA.Application.InvokeOnAppThread
due to the delegates.
How can i await the closing of the dialog?