【问题标题】:How do i read a byte array as a multi dimensional array in C#?如何在 C# 中将字节数组读取为多维数组?
【发布时间】:2019-08-22 00:21:41
【问题描述】:

我正在尝试使用 Python 将图像发送到 Unity C#,我想在其中显示叠加在当前显示器上的图像(对于每一帧)。 我目前正在使用 zmq 将 PNG 图像的字节数组从 python 接收到 Unity 中。 该图像采用形状为 256x256x4 的 numpy 数组的形式。使用np.tobytes函数,消息长度为2097152,大于(256x256x4)

消息发送到 Unity 后,我尝试使用以下代码 sn-p 从字节数组中获取颜色数组以使用 Texture2D.readPixels32()

var colorArray = new Color32[message.Length/4];
for(var i = 0; i < message.Length; i+=4)
{
    var color = new Color32(message[i + 0], message[i + 1], message[i + 2], message[i + 3]);
    colorArray[i/4] = color;
}
Texture2D tex2new=new Texture2D(Screen.width,Screen.height);
print (Screen.height + " " + Screen.width + " " + colorArray.Length);
tex2new.SetPixels32(colorArray);

错误是 SetPixels32 调用数组中的像素数无效 UnityEngine.Texture2D:SetPixels32(Color32[]) 如何将字节数组读入纹理? 在 Unity 中实时玩游戏后,有没有更好的方法来显示字节数组?

【问题讨论】:

  • 如何在python中读取png图像?你能显示打印的结果吗
  • 如果你想将 1d 转换为 2darray- 现在无法测试,但我做了类似的事情: // 获取和 x 和 y 坐标的索引 int index = x + ( y * w); or y + (x * w) // // 获取索引的 x 和 y int x = index % w; int y = Round((index / w) - 1); // 我现在不确定这个!?其中 w 是二维数组的 x 和 y 长度。如果 2darray 具有相同的 x 和 y 长度,则此方法有效。我不确定这是否是您需要的。

标签: c# python sockets unity3d textures


【解决方案1】:

以下内容对我有用:

Texture2D tex = new Texture2D(Screen.width/2, Screen.height,TextureFormat.RGBA32,false);
byte[] message = null;
bool gotMessage = false;
while (true)
{
    gotMessage = client.TryReceiveFrameBytes(out message); // this returns true if it's successful
    if (gotMessage) break;
}


tex.LoadImage(message);
byte[] mesg1 = tex.EncodeToPNG();
dest = cam2.targetTexture;
Graphics.Blit(tex, dest);
RenderTexture.active = dest;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多