Game Tech Tutorial
Published in

Game Tech Tutorial

How to integrate MessagePack C# for Unity? (.NET, XCode, IL2CPP)

MessagePack is widely used for games because of its performance. However, it’s not easy to integrate it with Unity due to a lack of documentation and tutorials.

In particular, there are plenty of problems you may encounter when running your app with MessagePack-CSharp library on the IOS device.

Even if you follow the documentation to set up the MessagePack-CSharp library, it might not be easy to make it work.

This tutorial aims to help you smoothly integrate MessagePack-CSharp for Unity.

Get Started

Environment:

  • MacBook Air
  • Unity 2019.3.14f with .NET 4.x
  • MessagePack C# v2.1.165
  • XCode v11.5

Check out the source codes from GitHub MessagePack C# and place it in your project as below.

Note:

  • Why not install a MessagePack package and related DLL files?
  • Because this way can reduce your time to find and install proper dependent DLL files for MessagePack.
  • Of course, you can manually install them if you want.

Step2:

Define the struct or class to be serialized and annotate.

Step3:

If you build your game in IL2CPP, you need to run dotnet mpc command to pre-generate codes for your class. Because Unity IL2CPP forbid runtime code generation.

First of all, install dotnet mpc command as below on your mac.

Then run below to test check it work properly.

Run the command below to generate codes for your class.

Note:

  • Assembly-CSharp.csproj is your C# project.
  • If you use the old version of Unity and select Monno in Script backend of Unity, you may not need to pre-generate codes.
  • MessagePack Library requires using C# v3.5+. If you use C# in 2.0, it's impossible to be compiled by Unity.

Step4:

Create a MessagePackStartup class as shown below to set up MessagePack lib.

MessagePackStatup.cs:

The most important part is you need to define proper Instances to register to MessagePackSerializerOptions as shown below.

What if you define it as below, what will happen?

You will encounter a problem below when running your game on the IOS device.

FormatterNotRegisteredException: Your Class is not registered in resolver:

Step5:

Build and run your game on the IOS device and test.

This’s all. I hope this tutorial can save your time and successfully integrate MessagePack C# for Unity.

Thanks for reading.

You might be interested in

--

--

We aim to save you time by offering highly technical tutorials and sharing our expertise, allowing you to concentrate on creating engaging game features rather than dealing with backend management or the burden of dump work.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eric Wei

Senior Full Stack Engineer & Solution architecture | AWS, GCP, Azure | Cloud, Unity Game Development, SDK, DevOps, and more.