Game Tech Tutorial
Published in

Game Tech Tutorial

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

MessagePack is wide 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

Step1:

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.

dotnet tool install --global MessagePack.Generator
dotnet new tool-manifest
dotnet tool install MessagePack.Generator

Then run below to test check it work properly.

dotnet mpc -h

Run the command below to generate codes for your class.

dotnet mpc -i Assembly-CSharp.csproj -o <your_output_path>/MessagePackGenerated.cs

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:

// MessagePackStartup.cs
using MessagePack;
using MessagePack.Resolvers;
using UnityEngine;
public class MessagePackStartup
{
static bool serializerRegistered = false;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Initialize()
{
if (!serializerRegistered)
{
StaticCompositeResolver.Instance.Register(
GeneratedResolver.Instance,
BuiltinResolver.Instance,
AttributeFormatterResolver.Instance,
MessagePack.Unity.UnityResolver.Instance,
PrimitiveObjectResolver.Instance,
MessagePack.Unity.Extension.UnityBlitWithPrimitiveArrayResolver.Instance,
StandardResolver.Instance
);
var option = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
MessagePackSerializer.DefaultOptions = option;
serializerRegistered = true;
}
}
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
static void EditorInitialize()
{
Initialize();
}
#endif
}

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

StaticCompositeResolver.Instance.Register(
GeneratedResolver.Instance,
BuiltinResolver.Instance,
AttributeFormatterResolver.Instance,
MessagePack.Unity.UnityResolver.Instance,
PrimitiveObjectResolver.Instance,
MessagePack.Unity.Extension.UnityBlitWithPrimitiveArrayResolver.Instance,
StandardResolver.Instance
);

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

StaticCompositeResolver.Instance.Register(
MessagePack.Resolvers.GeneratedResolver.Instance,
MessagePack.Resolvers.StandardResolver.Instance
);

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

--

--

--

High-quality technical tutorials: AWS, GCP, Unity, SDK, Security, and more.

Recommended from Medium

Splitting stories like a journalist

ELK Stack Setup in Azure with Fetch Data From EventHub

Zen Garden Development Plan

There’s Only One Way to Learn to Code

There’s only one way to learn to code graphic.

How fast is JavaScript to C or C#?

Fibit Play is the online version of the most played board game – Ludo, with various twists that you…

Poor Man’s Neuralink

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

Eric Wei

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

More from Medium

View Components in ASP.NET Core

Discovering a Revoked LG Monitor Firmware File

Self Signed Cert - Local Testing With ASP.NET Core WEB API

Authorizing Hangfire dashboard in .NET Web API using JWT tokens