Serialization stuff


Everybody wants their game to be optimized as much as possible. While developing a game I’ve faced with an issue when I had to synchronize something very large (let’s assume a List of Entity classes which have their own parameters inside, such as Age, BirthDate, Name, etc.); or I had to write save and load system. Of course you can say, why do I need to serialize that, if I just can pass the raw data of Entity. Well, theoretically you can, but what if Entity class has 10+ parameters and it is not just a single Entity but a bunch of them, would you write something similar to ” void SendEntityData(par1, par2, par3, par4, par5, par6, par7, par8, par9, par10, entity2par1, entity2par2 …” ? Apparently, not.

So what’s Serialization? It is the method of compressing your data into the smaller data which is nothing more than byte[]. That byte array can be deserialized into the original class which has been compressed previously.  There are a lot of serializators, you probably know some of them: JSON, BinaryFormatter, MessagePack, ProtoBuf etc.

Before choosing a serialization method you have to determine what do you need, in case if you need human-readability – I would suggest to use JSON or XML, but be careful with data size and perfomance do not pass this stuff in a Fast-paced multiplayer games. If human-readability is not the factor and you need the data to be encrypted and also high performance and smallest data size; I suggest ProtoBuf-net. You can and read more about it at:

If you previously used the BinaryFormatter then I would like to show you a little difference between it and ProtoBuf-net. In the following example, List of Entities has been written and read into and from the two different files over 1000+ times.

Full size image

As you can see, protobuf-net is 5x times faster and its file size 10x times less than BinaryFormatter.

Continue reading “Serialization stuff”


C# custom attributes

Suppose you do not want always check the distance between player and some object and instead of it you just want to mark your function like [DistanceAttribute(2f)] where 2f is allowable distance and your function will be runned only if that distance between player and object will be Less or Equal of 2 and method will run automatically without any “if” over and over.

Continue reading “C# custom attributes”

C# preprocessor directives with Unity (free project)

I’m pretty sure you do not want to include Server code to the Client build, for instance – your server code contains important SQL passwords or any sort of account information, instead of leading two different projects you could simply mark your snippet of code by #if Server and #if Client.

Continue reading “C# preprocessor directives with Unity (free project)”