October 08 2024

Budget-Friendly Serialization & Deserialization


If you are a game developer using Unity, you will eventually want to release a solid game that performs and is stable (am I assuming too much?). Or maybe you even already have launched one or multiple “successful” games.

One key area you must consider here is serialization and deserialization. That is, the process of persisting gameplay state across gaming sessions or levels. All games do that, even these little arcade games from the eighties “modern” players barely play anymore.

Problem is: choosing the wrong serialization & deserialization technology inexperienced devs tell you about will only bring you pain.

Let me help you make a better decision.

Serialization Matters to You (Even if You Don’t Know It Yet)

Serialization is crucial for two main reasons:

  1. Robustness: you don’t want to lose player progress or game state due to crashes or bugs. Additionally, you need to be able to update your game without breaking existing save games. There’s nothing like a furious player demanding a refund for your broken game because they lost the 1000 hours they spent on your game.

  2. Performance: you don’t want to freeze the game for a second every time you load or save a game, which can happen automatically even during gameplay. Or even worse, gift your players a 1-second freeze every time you update a single value in their game state.

I have seen it all. Fragile and bad performing serialization techniques that bring pain to players. After all, we help our studio clients fix wrong implementations all the time.

The Better Unity Serialization & Deserialization Techs?

A) For Prototyping

While prototyping, don’t overthink this. Almost everything works. Here I recommend the simplicity using of Unity’s built-in JSON serialization:

  1. Mark your classes/structures with the [Serialization] attribute
  2. Use JsonUtility.ToJson for serialization
  3. Use JsonUtility.FromJson for deserialization

This is perfect for things you want to throw away later on.

B) For Production Code

For production, you’ll want something more powerful AND robust. Something that supports versioning and doesn’t give you a free headache every morning which makes you quit gamedev and your relationship.

I recommend using either:

  • ProtoBuf.NET
  • Or… MessagePack

Efficient & robust solutions that work with binary formats to reduce your document output size and latency. Which is sexy for networking too by the way, not just save-games.

C) And for Your Project?

Of course, everything depends on your specific project and team. There are more options out there that I did not cover in this short video, even “creative” ways of using Scriptable Objects.

And there are more factors you must consider, such as

  • Garbage Collector allocations
  • Latency
  • Memory usage
  • Document output size
  • Data migrations

Don’t worry, the options I gave you are a good starting point. But if you want to make the BEST decision, then you must consider all these factors too.

Conclusion

I discuss serialization & deserialization with much deeper detail in the Performance Taskforce membership. Benchmarks, guides, benefits and blind spots of each tech (so, reasons not to choose each one). If you want to use serialization in production code, have a look at the Unity Performance Taskforce membership.

PerformanceTaskforce - December Module on XXXXX

And if your studio could use a hand from our specialized team of Unity consultants, let’s have a chat, we can help.

Ruben (The GameDev Guru)

The Gamedev Guru Logo

Performance Labs SL
Paseo de la Castellana 194, Ground Floor B
28046 Madrid, Spain

This website is not sponsored by or affiliated with Facebook, Unity Technologies, Gamedev.net or Gamasutra.

The content you find here is based on my own opinions. Use this information at your own risk.
Some icons provided by Icons8