Unity support
Summary
Nerdbank.MessagePack is ready for Unity, both for its mono and its IL2CPP backend. The NativeAOT readiness of this library makes it an ideal library for IL2CPP in your game.
Unity, for its part, is almost ready for Nerdbank.MessagePack. With a couple steps, you can make it work today.
Prerequisites
In addition to of course having Unity installed, you must upgrade the C# compiler it uses to a newer one that supports C# 12+. C# 12 introduced support for generic attributes, which is a fundamental requirement PolyType, upon which Nerdbank.MessagePack is based.
Use the UnityRoslynUpdater to upgrade the C# compiler.
In your unity project
Follow the instructions on UnityRoslynUpdater to enable C# 12+ language versions for your project. Nerdbank.MessagePack requires at least C# 12, though in general using the latest stable version is preferable.
Install NuGetForUnity into your project.
Use the new "NuGet -> Manage NuGet Packages" command in the Unity Editor to install the
Nerdbank.MessagePack
package. You may need to set the option to show prerelease versions.
Known issues
.NET Standard APIs only
Unity is currently limited to .NET Framework or .NET Standard 2.1 libraries. Some of the preferred APIs in Nerdbank.MessagePack are exposed uniquely to .NET 8+ projects. As a result, when reviewing documentation and samples for this library, be sure to look at the samples in their ".NET Standard" form.
Use of generic attributes
The UnityRoslynUpdater README warns against using generic attributes or your game will crash. This warning does not apply for uses of the GenerateShapeAttribute<T> attribute because the attribute is never included in your compiled assembly.
PolyType name collisions
If you encounter a compilation error suc as the following:
error CS0234: The type or namespace name 'ptSubject' does not exist in the namespace 'PolyType'"
Resolve it by taking the following steps:
- Turn off "Auto Reference" for the conflicting assemblies.
- Within the Unity Editor, navigate to the Assets\Packages\PolyType.*\lib\libstandard2.x folder in your Project.
- Select the PolyType.dll file.
- Within the Inspector, uncheck the "Auto Reference" option. Click Apply.
- Repeat these steps to turn off Auto Reference from Assets\Packages\Nerdbank.MessagePack.*\lib\netstandard2.x as well.
- Manually reference these two assemblies from your code.
- Within the Unity editor, create an Assembly Definition or select your existing one.
- In the Inspector, check the "Override References" option.
- Under Assembly References, click the Add button. Select the PolyType.dll assembly.
- Repeat the prior step, this time selecting to add a reference to the Nerdbank.MessagePack.dll.
- Scroll down as necessary in the Inspector and click Apply.