- Home /
Unity Transport API, how to writebytes & readbytes for texture data?
I'm trying to send texture/image bytes data to server. But keep getting ArgumentOutOfRangeException: Specified argument was out of the range of valid values. at stream.ReadBytes(array);
Please help guide me.
Client
public void SendBytes(string title, Texture2D _tex)
{
if(!m_Connected)
return;
byte[] buffer = _tex.GetRawTextureData();
var array = new NativeArray<byte>(buffer, Allocator.Temp);
Debug.Log("Send bytes with Length " + array.Length);
var writer = m_Driver.BeginSend(m_Connection);
writer.WriteString((NativeString64)title);
writer.WriteInt(array.Length);
writer.WriteBytes(array);
m_Driver.EndSend(writer);
}
Server
public void ProcessByteImage(DataStreamReader stream)
{
int bufferLength = stream.ReadInt();
Debug.Log("Get bytes with Length " + bufferLength);
Debug.Log("Stream Length = "+stream.Length);
byte[] recBuffer = new byte[stream.Length];
var array = new NativeArray(recBuffer, Allocator.Temp);
stream.ReadBytes(array);
recBuffer = array.ToArray();
texture2d = new Texture2D(2,2,TextureFormat.RGB24,false);
texture2d.LoadRawTextureData(recBuffer);
texture2d.Apply();
_rawImageBytes.texture = texture2d;
}
Comment
texture2d = new Texture2D(2,2,TextureFormat.RGB24,false);
This line hard coded expected raw data to be 12 bytes long. While it's not responsible for this exact exception but will be the next one :).
As to ArgumentOutOfRangeException: Specified argument was out of the range of valid values my guess is that array from stream.ReadBytes(array) is of length 0.
Becouse UDP can send only 65535 bytes in each packet. Unity use UDP.
Your answer