【问题标题】:Trouble invoking filepicker in hololens unity app在 hololens unity 应用程序中调用文件选择器时遇到问题
【发布时间】:2021-10-14 07:35:14
【问题描述】:

我一直在尝试通过一个统一应用程序上的一个按钮来调用本机 Hololens 文件选择器(虽然我真的不知道它是否有一个)。我正在统一使用 MRTK 来构建应用程序,这是我目前正在使用的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

#if ENABLE_WINMD_SUPPORT
using Windows.Storage;
using Windows.Storage.Streams;
#endif

public class OpenFilePicker : MonoBehaviour
{
    public void OpenFile()
    {

#if ENABLE_WINMD_SUPPORT
        FilePicker();
#endif
    }

    private async void FilePicker()
    {
#if ENABLE_WINMD_SUPPORT
        var picker = new Windows.Storage.Pickers.FileOpenPicker();
        picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
        picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
        picker.FileTypeFilter.Add(".jpg");
        picker.FileTypeFilter.Add(".jpeg");
        picker.FileTypeFilter.Add(".png");
        
        await picker.PickSingleFileAsync();
#endif
    }
}

点击按钮后,OpenFile() 被调用。代码编译和构建没有错误,但未能在 Hololens 2 模拟器上产生任何结果。我一直在互联网上搜索有关如何完成此操作的信息,但我发现的只是这个和一堆过时的帖子。

【问题讨论】:

标签: c# unity3d hololens


【解决方案1】:

试试这个,我遇到了一个非常相似的问题,我的代码构建良好,但是当我按下按钮时没有任何反应。我不得不更改 UWP 检查的类型。

using System;
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

public class OpenFilePicker : MonoBehaviour
{

    public void OpenFile()
    {
        FilePicker();
    }

    private void FilePicker()
    {
#if !UNITY_EDITOR && UNITY_WSA_10_0

        UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
        {
            var filepicker = new FileOpenPicker();
            filepicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filepicker.FileTypeFilter.Add(".jpeg");
            filepicker.FileTypeFilter.Add(".jpg");
            filepicker.FileTypeFilter.Add(".png");

            var file = await filepicker.PickSingleFileAsync();
            UnityEngine.WSA.Application.InvokeOnAppThread(() => 
            {
                // do something with file

            }, false);
        }, false);

#endif
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 2012-05-07
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 2011-09-20
    • 2011-08-23
    • 2012-08-15
    相关资源
    最近更新 更多