Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, April 25, 2013

Simple C# Unity Serializer

I wrote this class to serializes common unity types into and out of a byte array. Its nothing fancy, but its handy for networking player state.

     //--- Serialize ---  
     UnitySerializer srlzr = new UnitySerializer();  
     srlzr.Serialize(new Vector3(4, 8, 6));  
     byte[] byteArray = srlzr.ByteArray;  
   
     // send byte array somewhere..  
   
     //--- Deserialize ---  
     UnitySerializer dsrlzr = new UnitySerializer(byteArray);  
     Vector3 point3 = dsrlzr.DeserializeVector3();  

It supports Vector3, Vector2 and Quaternions, as well as the common primitives and its easy enough to extend to extra types. Just be sure to deserialize the data in the same order it was serialized. Check out the Example() function at the bottom to see how it works.