首页 > 解决方案 > Unable to read file in Hololens

问题描述

I am working on an app which needs to read .obj format files in the hololens at runtime.

Precisely, What I want to do is to save the .obj file in the hololens, when the app runs, it should access the file through the hololens file system. Putting the file in asset folder and loading by Unity editor is not what I want.

I have searched several forums and topic threads, many give quite similar solutions. I follow some sample codes and write my own codes to read files in hololens. However, it doesn't work.

The relevant parts of my program are as follows:

At the head of script file, import namespace

    #if WINDOWS_UWP
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using System.Threading.Tasks;
    #endif

The script which load obj file receives Stream type parameter. So I implement this function "OpenFileForRead", which receives a folder path and a file name as parameter, returns file stream.

    private static Stream OpenFileForRead(string folderName, string fileName)
    {
        Stream stream = null;
#if WINDOWS_UWP
        Task task = new Task(
        async () => {
            StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
            StorageFile file = await folder.GetFileAsync(fileName);
            stream = await file.OpenStreamForReadAsync();
        });
        task.Start();
        task.Wait();
#endif
        return stream;
    }

And I call this function as follows:

#if WINDOWS_UWP
        string objname = "Model.obj";
        Stream stream = OpenFileForRead(ApplicationData.Current.RoamingFolder.Path, objname);
        loadedObj = new OBJLoader().Load(stream);
#endif

"ApplicationData.Current.RoamingFolder.Path" is mapped to "LocalAppData\SOMEAPP\RoamingState" in the hololens.

So when I deployed this app in hololens, I will upload the "Model.obj" file in device portal, to this app's RoamingState folder. Then I run this app, under ideal circumstance, it should work, unfortunately it doesn't.

The current situation is, the app can be successfully deployed to hololens and run. Other Gameobjects can function normally in hololens, though it fails to load the .obj file into scene.

By the way, " new OBJLoader().Load(stream); " has been tested in Unity Editor, which can successfully read and load .obj file in local folder on my PC. So it is most likely that the problem lies in the way I read files in hololens. But I surely follows the solutions given by other similar topics. I am confused and have no idea what exactly is going wrong?

Here are some websites I have referenced and copied some codes from:

https://longqian.me/2017/02/08/hololens-file-transfer/

https://forums.hololens.com/discussion/1862/how-to-deploy-and-read-data-file-with-app

https://github.com/Maximilian-Winter/ObjHololensViewer

https://github.com/jmher019/Hololens-OBJRenderer

Nearly all above use similar codes to read files in hololens, though I can't get it through on my device, it is so frustrating!

Untiy Editor: 2018.4.0f1

Hololens: 1st gen

标签: c#unity3duwphololenswindows-mixed-reality

解决方案


在使用 Hololens (v1) 和 Unity 读取数据之前,我遇到了很多问题。我花了很长时间才弄清楚原因……尤其是当我发现这是 Unity 中的一个错误时。我检查了我的旧论坛帖子,您的 Unity 版本仍然存在此问题。

请参阅:https ://forum.unity.com/threads/how-to-access-files-folders-on-hololens-with-il2cpp.543022/

请注意,来自 Unity 的人(几乎是最后一篇文章)说:

是的。该修复程序登陆2019.1.8f12018.4.3f1。它还没有 > 降落到 2019.2 或 2019.3。

只是落后了一点。我会正确备份您的项目,并尝试安装新的 Unity。我记得在升级使所有 Visual Studio 工具再次正常工作后,我也遇到了问题(与 Win10 SDK 版本有关?)。

最后,我不得不更改 XML 清单文件中的权限,因为 Unity 没有添加正确的权限,即使在应用程序设置中更改它也是如此。不幸的是,我不记得到底是什么,并且由于在家工作,现在无法完成这个项目。

编辑:我在 2019.1 发行说明中找到了对修复的引用。我在 2018.x 中找不到它:

https://unity3d.com/unity/whats-new/2019.1.8

通用 Windows 平台:修复了 System.IO API 无法处理 IL2CPP 脚本后端上的应用程序和 AppData 目录之外的文件。(1063768, 1156200)

而实际的问题跟踪器问题:

https://issuetracker.unity3d.com/issues/uwp-il2cpp-file-dot-exists-method-returns-false

我认为这也可能是您的问题。


推荐阅读