Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Struct Serialisation

Discussion in 'Assets and Asset Store' started by returnString, Aug 2, 2011.

  1. returnString

    returnString

    Joined:
    Jul 10, 2010
    Posts:
    248
    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):

    Code (csharp):
    1. public struct DamageInfo
    2. {
    3.     public float damage;
    4.     public DamageType damageType;
    5.     //Constructors
    6. }
    7.  
    8. public enum DamageType
    9. {
    10.     Standard,
    11.     LevelReset
    12.     //Insert more options here
    13. }
    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:

    Code (csharp):
    1. //Pack the data and send it
    2. void Test()
    3. {
    4.     byte[] byteArray = Serialisation.SerialiseStruct(new DamageInfo(100, DamageType.Standard)));
    5.     networkView.RPC("TestStruct", RPCMode.All, byteArray);
    6. }
    7.  
    8. //Receive the data, now we can work locally with the DamageInfo struct itself
    9. [RPC]
    10. void TestStruct(byte[] bytes)
    11. {
    12.     DamageInfo damage = Serialisation.DeserialiseStruct<DamageInfo>(bytes);
    13. }
    14.  
    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? ;)).
     
    Last edited: Aug 2, 2011
  2. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    wait wait wait wait. RPC's can use byte arrays?
     
  3. returnString

    returnString

    Joined:
    Jul 10, 2010
    Posts:
    248
    Indeed. :)

    I haven't tested with anything particularly large in terms of values yet but I imagine you can get a fair bit of data in there.

    Edit: There's also a string version of the SerialiseStruct method available if for whatever reason you prefer that.
     
    Last edited: Aug 2, 2011
  4. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    Well that's good to know. I've been serializing using base64 encoding, which is far less compact than just straight byte arrays.

    Which probably relates to the fact that I discovered a single string parameter in an rpc cannot be more than 4096 characters, so that's probably the limit for byte[] as well (however many bytes a character is * 4096)