简体   繁体   中英

Unity and Hololens: Reading non-text file exception

[Edited] First I have to apologize, I've just noticed the bit of code I pasted as our code for reading png files is not working in .net backend either (the txt reading does work for both, .net and il2cpp, as stated). The call stack cames from a different exception. Is corrected now

We are working in a XR application for Microsoft Hololens which involves reading both, txt and binary files (the latter as byte arrays to be loaded as Unity textures by Texture2D.LoadImage). As we need to provide an easy way for the user to change/modify the files both are located in the 3D Objects folder in the hololens.

Everything was working properly until we recently had to change our scripting backend from .net to il2cpp; since then we are finding errors whenever we try to read our png files though txt can still be readed with no changes in our .net source code.

We are reading our text files like this (working properly):

    Windows.Storage.StorageFolder objectsFolder = Windows.Storage.KnownFolders.Objects3D;
    Windows.Storage.StorageFile csvFile = await objectsFolder.GetFileAsync(oneFile);
    string contentText = await Windows.Storage.FileIO.ReadTextAsync(csvFile);

For the PNGs we tried several different approachs, form the simplest File.ReadAllBytes( pngFile.Path); (it works for .net but not for il2cpp), FileStream and other methods but they always fail at some point. The last one is this:

     Windows.Storage.StorageFolder pngObjectsFolder = Windows.Storage.KnownFolders.Objects3D;
     Windows.Storage.StorageFile pngFile = await pngObjectsFolder.GetFileAsync(i.ToString() + ".png");

     Windows.Storage.Streams.IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(pngFile);
     Windows.Storage.Streams.DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(buffer);


     dataReader.ReadBytes (fileData);

... which throws this exception:

Exception thrown: 'System.NullReferenceException' in Assembly-CSharp.dll

and have only this in the call stack:

at CsvReader.d__4.MoveNext()

So it seems to us that our dataReader stays null but we don't really understand what is happening neither how to prevent it. Can anyone provide some advice or ideas for reading such kind of file?

regards!

After some more work we finally found why this wasn't working... and it's embarrassing. It happens that the array 'fileData' wasn't initializated. The source code should be this:

        Windows.Storage.StorageFolder pngObjectsFolder = Windows.Storage.KnownFolders.Objects3D;
        Windows.Storage.StorageFile pngFile = await pngObjectsFolder.GetFileAsync(i.ToString() + ".png");             
        Windows.Storage.Streams.IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(pngFile);                      
        Windows.Storage.Streams.DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer (buffer);

        fileData = new byte[buffer.Length];     
        dataReader.ReadBytes (fileData);

At that point we have the png file in fileData as a byte array and we are free to do whatever we need with it. In our case, load it into an Unity 3d texture object:

        tex = new Texture2D(2, 2);
        tex.LoadImage(fileData);

So in case anybody else is struggling with loading binary files with il2cpp backend this seem to be a reliable approach.

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