0

I'm trying to open a PDF within a Unity app deployed to a Microsoft Hololens 2 using the MRTK. I want to use the integrated Edge Browser, that is able to load within the app and to run simulatenously. Edge by itself is also able to load pdf files. I expected using LaunchUri would open the file from a provided location

    public void Launch()
    {
        var absolutePath = Path.Combine(_myDocuments, _subfolder);
        absolutePath = Path.Combine(absolutePath, _fileName);            
#if UNITY_WSA
        UnityEngine.WSA.Launcher.LaunchUri(absolutePath, true);
#else
        Application.OpenURL(absolutePath);
#endif
    }

Launching the Edge Browser with a website as the string, works. Entering the file path manually into the browser window to load the pdf also works, but launching the Browser by directly providing the full path to my file, does not open any browser window. I suspect, that it doesn't work because Edge is not setup to be the default app for PDF files, but when using the button within Edge to make it the default app, it just tells me that it failed and I can't find any other way to achieve that. Does anyone else have an idea how I would be able to load Edge from within an Unity app and providing the path to my pdf file so it automatically loads on a button press?

The PDF is stored on the local file system under "Documents". As mentioned, the file is accessible and can be opened by Edge. Using the system File Browser to manually open the PDF from within the system automatically uses Edge and opens the file as expected, which is the behaviour I would like to copy from within Unity.

Environment

  • Unity 2020.3.15f
  • Hololens 2 latest Update
  • Win10 SDK target 10.0.19041.0
  • Build to ARM64 for Hololens

edit: I ended up using

UnityEngine.WSA.Launcher.LaunchFile(folderType, relativePath, false);
Senbazuru
  • 381
  • 3
  • 14
  • It's hard for us to narrow down issue without the code snippets, could you please shall the sample code and show us the path you put the pdf file? Also this HoloLens OS version and Unity version is important, could you please share that? – Franklin Chen - MSFT Feb 21 '22 at 04:54
  • Of course. I added the Info you requested to the question. – Senbazuru Feb 21 '22 at 10:02
  • Try changing the path separator from "/" to"\\" or vice-versa. – Kitswas Feb 21 '22 at 10:05

1 Answers1

1

You can try Launcher.LaunchFileAsync to launch a PDF file with Edge. This method required a StorageFile as the parameter, which can be got by FileOpenPicker or GetFileFromPathAsync.

Please refer to the following code.

    private async void PickAndLaunchFile()
    {
        // First, get a file via the picker.
        var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
        openPicker.FileTypeFilter.Add("*");
        Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            // Next, launch the file.
            bool success = await Launcher.LaunchFileAsync(file);
    }
Hernando - MSFT
  • 2,895
  • 1
  • 5
  • 11
  • If I'm not mistaken, the Windows namespace is not available for UWP Unity builds. Also, LaunchFileAsync also uses the default app, which, as mentioned cannot be set for PDF files on the Hololens2 in my case. – Senbazuru Feb 22 '22 at 11:04
  • It is possible to use WinRT API directly in Unity scripts, please see: [WinRT APIs with Unity for HoloLens](https://learn.microsoft.com/en-us/windows/mixed-reality/develop/unity/using-the-windows-namespace-with-unity-apps-for-hololens). I tested this API on the UWP HoloLens app, it can launch the PDF file with Edge, even without setting it as the default application. Also, AFAIK, only the files come from your app's package can be referred as a URI:[ms-appx and ms-appx-web](https://learn.microsoft.com/en-us/windows/uwp/app-resources/uri-schemes#ms-appx-and-ms-appx-web) – Hernando - MSFT Feb 23 '22 at 06:47
  • Thank you for the tip, it works now, although I ended up using: UnityEngine.WSA.Launcher.LaunchFile(folderType, relativePath, false); – Senbazuru Feb 23 '22 at 12:04