14
12

More than 5 years have passed since last update.

MessagePack for C# を Unity iOS で使う時のメモ

Posted at

これはなに

MessagePack for C# を Unity の iOS で使うとき一癖あったのでそのメモです。

環境

  • macOS Mojave 10.14.1
  • Unity 2018.2.10f1
  • Mono JIT compiler version 5.12.0.301
  • MessagePack-CSharp Ver 1.7.3

Import

ここは普通です。

  1. https://github.com/neuecc/MessagePack-CSharp/releases/tag/v1.7.3 から MessagePack.Unity.1.7.3.zip をダウンロード
  2. unitypackage を開いてインポートする

Attribute をつける

README に書いてあるとおり、シリアライズしたい class / struct に [MessagePackObject] を、 member / property に [Key] 属性をつけます。
https://github.com/neuecc/MessagePack-CSharp#object-serialization

コードの事前生成

ここがハマりポイントです。

README を読むと、 Unity iOS (IL2CPP) ではコードの事前生成が必要なことがわかります。
https://github.com/neuecc/MessagePack-CSharp#pre-code-generationunityxamarin-supports

macOS では mono サポートがあると書いてありますが、上記 v1.7.3 に含まれる mpc.exe は正常に動きません(中身が空の Resolver が生成されます)。

この Issue comment にある fork のリリース に含まれている tools を使うとうまくいきます。

しかし最新の mono の stable (5.16.0)では動きませんでした。
とりあえず動かしたいので archive から少し古いビルドをインストールします(今回は 5.12.0.301 で試しました)。

これで生成できます。
mono path/to/MessagePack.Unity.1.7.3/tools/mpc.exe -i Assembly-CSharp.csproj -o Assets/Scripts/Generated/MessagePackGenerated.cs

事前生成した Resolver を登録する

今回は1シーンのプロジェクトだったので、雑にシーンに直置きしました。

MessagePackResolverRegister.cs
using MessagePack.Resolvers;
using UnityEngine;

public class MessagePackResolverRegister : MonoBehaviour
{
    void Start()
    {
        CompositeResolver.RegisterAndSetAsDefault(
            GeneratedResolver.Instance,
            MessagePack.Unity.UnityResolver.Instance,
            BuiltinResolver.Instance,
            AttributeFormatterResolver.Instance,
            PrimitiveObjectResolver.Instance
            );
    }
}

さいごに

ちゃんと修正してコントリビュートしたい人生だった。

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12