1

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?

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Greta-A
  • 43
  • 5
  • Please provide more information about your developing environment, such as the Version number of your Unity, the Project Setting and Build Setting of Unity, and the HoloLens System version, and you ought to check whether an app installed (such as OneDrive) that allows picking. – Hernando - MSFT Aug 31 '21 at 07:08
  • I'm using Unity 2020.2.3 on UWP platform and HoloLens 2 version 21H1. The 2 samples I mention in my post work without a FilePicker app installed, so I know it's possible without one. – Greta-A Aug 31 '21 at 15:51
  • I made a little modification to your code to make it execute after clicking the button. And then, deployed it to the device for testing, it worked well for me. I am using Unity 2020.3.1f, could you upgrade Unity and try again? If you see the same behavior, please provide an MVCE(stackoverflow.com/help/minimal-reproducible-example) for reproducing, so that we can locate the issue or find a solution. – Hernando - MSFT Sep 01 '21 at 06:11

1 Answers1

1

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
    }

...
Greta-A
  • 43
  • 5