简体   繁体   中英

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

I have had quite some issues before with the Hololens (v1) and Unity, reading data. It took me ages to figure out the cause... especially when I discovered that it was a bug in Unity. I checked my old forum posts, and you have a Unity version that still suffers from this.

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

Please note that the person from Unity (almost last post) says:

Yeah, I do. The fix landed to 2019.1.8f1 and 2018.4.3f1 . It hasn't > landed to 2019.2 or 2019.3 yet.

You are just a little bit behind that. I would backup your project properly, and try installing a new Unity. I remember I also suffered from issues after upgrading getting all the Visual Studio tools properly working again (something related to Win10 SDK versions?).

Finally, I had to change the permissions in a XML manifest file as Unity did not add the proper permissions, even when changing it in the app settings. Unfortunately I cannot remember what exactly, and cannot reach this project right now due to working from home.

Edit: I have found the reference to the fix in the 2019.1 release notes. I could not find it in the 2018.x one:

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

Universal Windows Platform: Fixed System.IO APIs not working on files outside of application and AppData directories on IL2CPP scripting backend. (1063768, 1156200)

And the actual issuetracker issue:

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

I think this is likely your issue too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM