【发布时间】:2014-02-02 15:21:22
【问题描述】:
将array 的Color32[] 值复制/转换 到byte[] 缓冲区的快速方法是什么?
Color32 是 Unity 3D 中包含 4 bytes, R, G, B and A respectively 的结构。
我想要完成的是通过管道将渲染图像从统一发送到另一个应用程序(Windows Forms)。目前我正在使用此代码:
private static byte[] Color32ArrayToByteArray(Color32[] colors)
{
int length = 4 * colors.Length;
byte[] bytes = new byte[length];
IntPtr ptr = Marshal.AllocHGlobal(length);
Marshal.StructureToPtr(colors, ptr, true);
Marshal.Copy(ptr, bytes, 0, length);
Marshal.FreeHGlobal(ptr);
return bytes;
}
谢谢你,对不起,我是 StackOverflow 的新手。 马林内斯库·亚历山德鲁
【问题讨论】:
-
所以你已经得到了那个代码......它做你想要的吗?
-
能把你的结果、问题和你的问题分享清楚吗?!
-
看起来是这样。但我想知道是否存在更快的方法......
-
有什么速度提升吗?
-
我将通过管道从 Unity 渲染的每一帧发送到另一个应用程序。这就是为什么我想知道是否存在更快的方法。目前,我每帧大约需要 11 毫秒来将 Color32[] 数组转换为 byte[] 数组。以前我使用 EncodeToPNG() 方法,每帧大约需要 85 毫秒。
标签: c# struct copy unity3d marshalling