Skip to content

Commit

Permalink
fix: NetworkReader/Writer Texture2D now sends dimensions too. fixes "…
Browse files Browse the repository at this point in the history
…Texture2D.SetPixels32: size of data to be filled was larger than the size of data available in the source array. (Texture '')"
  • Loading branch information
miwarnec committed Apr 29, 2022
1 parent d235037 commit b709453
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Assets/Mirror/Runtime/NetworkReaderExtensions.cs
Expand Up @@ -271,8 +271,17 @@ public static Uri ReadUri(this NetworkReader reader)

public static Texture2D ReadTexture2D(this NetworkReader reader)
{
Texture2D texture2D = new Texture2D(32, 32);
texture2D.SetPixels32(reader.ReadArray<Color32>());
// TODO allocation protection when sending textures to server.
// currently can allocate 32k x 32k x 4 byte = 3.8 GB

// read width & height
short width = reader.ReadShort();
short height = reader.ReadShort();
Texture2D texture2D = new Texture2D(width, height);

// read pixel content
Color32[] pixels = reader.ReadArray<Color32>();
texture2D.SetPixels32(pixels);
texture2D.Apply();
return texture2D;
}
Expand Down
7 changes: 7 additions & 0 deletions Assets/Mirror/Runtime/NetworkWriterExtensions.cs
Expand Up @@ -291,6 +291,13 @@ public static void WriteUri(this NetworkWriter writer, Uri uri)

public static void WriteTexture2D(this NetworkWriter writer, Texture2D texture2D)
{
// TODO allocation protection when sending textures to server.
// currently can allocate 32k x 32k x 4 byte = 3.8 GB

// write dimensions first so reader can create the texture with size
// 32k x 32k short is more than enough
writer.WriteShort((short)texture2D.width);
writer.WriteShort((short)texture2D.height);
writer.WriteArray(texture2D.GetPixels32());
}

Expand Down

0 comments on commit b709453

Please sign in to comment.