lime雑記

ゲーム開発、その他雑記。

【Unity】MessagePack for C#でのコード事前生成

この記事でのバージョン

Unity 2021.3.11f1
MessagePack for C# v2.5.108

概要

github.com

MessagePack for C#を使用し以下のようにSerializeを行うと、IL2CPPでビルドしたときにエラーが出ます。

[MessagePackObject]
public class TestData {
  [Key(0)]
  public int A { get; set; }

  [Key(1)]
  public float B { get; set; }

  [Key(2)]
  public string C { get; set; }
}

public class MessagePackTest : MonoBehaviour {
  public void Serialize() {
    var data = new TestData();
    byte[] serializeData = MessagePackSerializer.Serialize(data);
  }
}

(以下のエラーが出る)

FormatterNotRegisteredException: TestData is not registered in resolver: MessagePack.Resolvers.StandardResolver

解決には、事前のコード生成というものが必要になります。

解決方法

1..NETのインストール

以下のサイトから.NETをインストールします。
(.NET 7.0で動作確認済み)
dotnet.microsoft.com

2.mpc(MessagePackCompliler)のインストール

コマンドプロンプトから以下のコマンドを実行してインストールします。

dotnet tool install --global MessagePack.Generator

※エラーが出る場合はPCを再起動するとうまくいく場合があります

3.mpc(MessagePackCompliler)の実行
  • iオプションにSerialize対象のクラスがあるcsproj、-oに生成されるコードの出力パスを記述します。
mpc.exe -i Scripts.csproj -o ./Assets/Script/Serializer.generated.cs

Serialize対象のクラスにメンバ追加などの変更があるたび実行する必要があるため、batファイルなどにしておくことをおすすめします。

4.Resolverの設定

任意のクラスに下記関数を追加します。

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Initialize() {
  StaticCompositeResolver.Instance.Register(
    MessagePack.Resolvers.GeneratedResolver.Instance,
    MessagePack.Resolvers.StandardResolver.Instance
  );
  var option = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
  MessagePackSerializer.DefaultOptions = option;
}
5.再ビルド

この状態でビルドすればエラーが出なくなります。