c# - 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!
解决方案
经过更多的工作,我们终于找到了为什么这不起作用……这很尴尬。碰巧数组'fileData'没有初始化。源代码应该是这样的:
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);
那时,我们在 fileData 中将 png 文件作为一个字节数组,我们可以随意使用它做任何我们需要的事情。在我们的例子中,将其加载到 Unity 3d 纹理对象中:
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
因此,如果其他人正在努力使用 il2cpp 后端加载二进制文件,这似乎是一种可靠的方法。
推荐阅读
- javascript - 如何在 mvc 应用程序中为 @Html.EditorFor 编写 javascript onfocusout/onblue 事件
- arrays - 如何从八度数组中读取一些固定数量的元素?
- r - R read.table() 不包括第一列?
- typescript - 使用 Mithril.js,如何将异步函数给出的选项添加到先前添加的选择元素
- excel - VBA excel宏跳过最后一列
- c# - 如何将异常的 json 表示形式转换回 c# 异常?
- javascript - 如何在今天做出反应测试。getFullYear() + 1
- spring-boot - 动态加载IntegrationFlow
- java - 如何解决“方法 org.postgresql.jdbc.PgConnection.createBlob() 尚未实现”
- amazon-web-services - AWS VPC 及其子网的 CIDR 块