I'm trying to find out if it's possible to use the native HoloLens file picker in an immersive Unity app. I've tested 2 samples so far that both work, but only if they're using a UI window for the entire app. I've been following other people's SO posts and created this:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
#if WINDOWS_UWP
using System;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.System;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.Storage.Pickers;
#endif
...
void Start()
{
#if WINDOWS_UWP
SelectJSON();
#endif
}
#if WINDOWS_UWP
void SelectJSON()
{
UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
{
debug.text = "starting filePicker";
var filepicker = new FileOpenPicker();
filepicker.FileTypeFilter.Add(".json");
debug.text = "waiting for single file";
var file = await filepicker.PickSingleFileAsync();
UnityEngine.WSA.Application.InvokeOnAppThread(() =>
{
debug.text = "about to read data";
ReadData(file);
}, false);
}, false);
}
#endif
In my app, I get to the 'new FileOpenPicker()' part, and that's where the code hangs. I believe that the app is trying to open the file explorer window, but since I'm in an immersive Unity app, it just hangs there since there's no response from the user. I've put in a try/catch statement previously, but there weren't any errors or exceptions. I have tried various settings in Unity, like Default FullScreen or Run In Background, but none of those did anything. Has anyone got the native FilePicker to work in their Unity app or can tell me what I'm doing wrong?
So after alot of troubleshooting, I found something that works. I'm using Unity 2020.3.13, but it'll work on an older version too. The only thing that is different is the type of check for UWP. This brings up the file explorer in my immersive app and allows me to choose the file I want and continue.
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if WINDOWS_UWP
using System;
using Windows.Storage;
using Windows.System;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.Storage.Pickers;
#endif
...
void Start()
{
OpenFile();
}
public void OpenFile()
{
#if !UNITY_EDITOR && UNITY_WSA_10_0
UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
{
var filepicker = new FileOpenPicker();
filepicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filepicker.FileTypeFilter.Add(".json");
var file = await filepicker.PickSingleFileAsync();
UnityEngine.WSA.Application.InvokeOnAppThread(() =>
{
ReadData(file);
}, false);
}, false);
#endif
}
...