This is a native plug-in that divides a given data (System.IntPtr or array) into specified sizes and restores them regardless of the input order. It is intended for use cases such as sending large data via UDP.
- Unity Package
- Download the latest .unitypackage from Release page.
- Git URL (UPM)
- Add
https://github.com/hecomi/uPacketDivision.git#upmto Package Manager.
- Add
- Scoped Registry (UPM)
- Add a scoped registry to your project.
- URL:
https://registry.npmjs.com - Scope:
com.hecomi
- URL:
- Install uPacketDivision in Package Manager.
- Add a scoped registry to your project.
Currently, it is only built for Windows.
Create a Divider and call Divide<T>(T[]) with the array as input (or Divide(System.IntPtr, int size)). This will divide the packet internally.
Dividerdivider=newDivider();voidDivide(){Texture2Dimage;varpixels=image.GetPixels32();divider.maxPacketSize=packetSize;divider.Divide(pixels);}Then, send the split data to the remote in some way. The following functions are available.
GetChunkCount().- the number of chunks.
GetChunk()- The
byte[]array of the split data
- The
GetChunkSize(int index).- The size of the split data.
GetChunkData(int index).- The pointer of the split data Here is an example of using uOSC.
uOSC.uOscClientclient;voidSend(intwidth,intheight){client.Send("/Size",width,height);for(uinti=0;i<divider.GetChunkCount();++i){client.Send("/Data",divider.GetChunk(i));}}If you want to use the pointer and size directly, please use GetChunkSize() and GetChunkData() instead.
Use Assembler to assemble the data sent to you. Here is an example of the receiving part using uOSC.
Assemblerassembler=newAssembler();Texture2Dtexture;publicvoidOnDataReceived(uOSC.Messagemessage){if(message.address=="/Size"){varw=(int)message.values[0];varh=(int)message.values[1];OnSize(w,h);}elseif(message.address=="/Data"){vardata=(byte[])message.values[0];OnData(data);CheckEvent();}}voidOnSize(intw,inth){texture=newTexture2D(w,h);}voidOnData(byte[]data){assembler.timeout=timeout;assembler.Add(data);}Each time you add data, check for the completion or loss as follows.
voidCheckEvent(){switch(assembler.GetEventType()){caseEventType.FrameCompleted:{OnDataAssembled(assembler.GetAssembledData<Color32>());break;}caseEventType.PacketLoss:{vartype=assembler.GetLossType();Debug.LogWarning("Loss: "+type);break;}default:{break;}}}If you want to get a pointer and its size instead of an array, the following APIs are available.
varindex=assembler.GetAssembledFrameIndex();vardata=assembler.GetFrameData(index);varsize=assembler.GetFrameSize(index);OnDataAssembled(data,(int)size);assembler.RemoveFrame(index);Then, the data reconstruction will be completed as follows.
voidOnDataAssembled(Color32[]pixels){texture.SetPixels32(pixels);texture.Apply();GetComponent<Renderer>().material.mainTexture=texture;}