Texture2D.GetData方法的解决方法 [英] Workaround for Texture2D.GetData method
问题描述
我正在使用Monogame将游戏从XNA转换为iOS。
在下面的代码段中, smallDeform
是 Texture2D
我在其上调用 GetData
方法。
I’m converting a game from XNA to iOS with Monogame.
In the code snippet below, smallDeform
is a Texture2D
on which I call the GetData
method.
smallDeform = Game.Content.Load<Texture2D>("Terrain/...");
smallDeform.GetData(smallDeformData, 0, smallDeform.Width * smallDeform.Height);
我在使用Monogame时遇到一些问题,因为iOS中的功能尚未实现,因为它返回这个例外。
I’m having some problem with Monogame because the feature in iOS has not been implemented yet because it returns this exception.
#if IOS
throw new NotImplementedException();
#elif ANDROID
我试图将来自Windows的XML文件中的数据序列化为加载来自iOS的整个文件。序列化文件每个重量超过100MB,这是非常不能解析的。
I tried to serialize the data in a XML file from Windows to load the whole file from iOS. The serialized files weight more than 100MB each, that is quite unacceptable to parse.
基本上,我正在寻找一种解决方法来获取数据(例如 uint []
或 Color []
)来自纹理而不使用 GetData
方法。
Basically, I’m looking for a workaround to get the data (for instance a uint[]
or Color[]
) from a texture without using the GetData
method.
PS:我在Mac上,所以我不能使用Monogame SharpDX库。
PS: I'm on Mac, so I can't use the Monogame SharpDX library.
谢谢事先。
推荐答案
我回答我自己的问题,如果有人会遇到同样的问题。
I answer my own question, if someone will have the same problem.
按照 craftworkgame 的建议,我使用字节流保存了数据,但由于缺少 File
类,我不得不使用WinRT函数:
Following craftworkgame's suggestion, I saved my data using byte streams, but due to the absence of File
class, I had to use WinRT functions:
private async void SaveColorArray(string filename, Color[] array)
{
StorageFile sampleFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(filename + ".dat", CreationCollisionOption.ReplaceExisting);
IRandomAccessStream writeStream = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream outputSteam = writeStream.GetOutputStreamAt(0);
DataWriter dataWriter = new DataWriter(outputSteam);
for (int i = 0; i < array.Length; i++)
{
dataWriter.WriteByte(array[i].R);
dataWriter.WriteByte(array[i].G);
dataWriter.WriteByte(array[i].B);
dataWriter.WriteByte(array[i].A);
}
await dataWriter.StoreAsync();
await outputSteam.FlushAsync();
}
protected async Task<Color[]> LoadColorArray(string filename)
{
StorageFile sampleFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(filename + ".dat", CreationCollisionOption.OpenIfExists);
IRandomAccessStream readStream = await sampleFile.OpenAsync(FileAccessMode.Read);
IInputStream inputSteam = readStream.GetInputStreamAt(0);
DataReader dataReader = new DataReader(inputSteam);
await dataReader.LoadAsync((uint)readStream.Size);
Color[] levelArray = new Color[dataReader.UnconsumedBufferLength / 4];
int i = 0;
while (dataReader.UnconsumedBufferLength > 0)
{
byte[] colorArray = new byte[4];
dataReader.ReadBytes(colorArray);
levelArray[i++] = new Color(colorArray[0], colorArray[1], colorArray[2], colorArray[3]);
}
return levelArray;
}
这篇关于Texture2D.GetData方法的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!