Just a small code package that I created recently for The Adventures of Box Guy, thought the community might find it useful so I uploaded it to the Asset Store. ![]()
Unity’s built-in RPC function is only capable of sending int, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion types across the network. This is fine for most situations, but what if you need to send a slightly more complex piece of data across the network? I’ll give an example.
Certain entities in Box Guy have health attributes, and damage is sent as a DamageInfo struct to be processed by the entity itself, which looks like this (unnecessary stuff represented with comments as to save space):
public struct DamageInfo
{
public float damage;
public DamageType damageType;
//Constructors
}
public enum DamageType
{
Standard,
LevelReset
//Insert more options here
}
Now custom structs can’t be sent across the network, meaning we needed a workaround in order to get these transmitted across the network (including the enums). In the end I wrote a quick serialisation class to handle packing and unpacking these with generics so they’re totally reusable.
To serialise, send and then receive the struct with this little class, you can do:
//Pack the data and send it
void Test()
{
byte[] byteArray = Serialisation.SerialiseStruct(new DamageInfo(100, DamageType.Standard)));
networkView.RPC("TestStruct", RPCMode.All, byteArray);
}
//Receive the data, now we can work locally with the DamageInfo struct itself
[RPC]
void TestStruct(byte[] bytes)
{
DamageInfo damage = Serialisation.DeserialiseStruct<DamageInfo>(bytes);
}
The serialisation itself is written in C# but naturally the code works with both UnityScript and C#, and examples for both are included. All the code in the package is fully documented (with XML-style C# comments in case you’re outputting documentation for your project) and if you have any queries, feedback or even suggestions for future features just let me know via support@inkdev.net or post here.
It’s available from the Asset Store for the princely sum of 5 dollars (4.75 euros) and any money goes to the licence fund for future Box Guy upgrades (Box Guy mobile, anyone? ;)).