环境配置

首先打开主页查看文档:插件Github链接
可以看到有这么一句话:
The minimum supported Unity version will be 2022.3.12f1, as it is necessary to support IL2CPP via C# Source Generator.
Unity 最小支持版本为 2022.3.12f1,因为支持 IL2CPP 通过 C# 源生成器是必要的。

在上述的基础环境下开始配置:

  1. 打开包管理器,添加来自git URL的包:https://github.com/GlitchEnzo/NuGetForUnity.git?path=/src/NuGetForUnity
  2. 此时菜单栏多出来一个NuGet,打开NuGet-Manage NuGet Packages,在搜索框输入MessagePack,将同名插件进行安装
  3. 打开包管理器,添加来自git URL的包:https://github.com/MessagePack-CSharp/MessagePack-CSharp.git?path=src/MessagePack.UnityClient/Assets/Scripts/MessagePack
  4. 此时最麻烦的环境配置就已经大功告成了

热更处理

在你的热更新入口添加如下代码,此代码越早加载越好。

    private void Awake()
    {
        //初始化 MessagePack
        InitMessagePack();
    }

    private void InitMessagePack()
    {
        // Register resolvers. Order matters: custom/generated first, then Unity, then standard/contractless.
        StaticCompositeResolver.Instance.Register(
            // If Source Generator is used, keep it first
            MessagePack.GeneratedMessagePackResolver.Instance,
            // Unity specific types
            MessagePack.Unity.UnityResolver.Instance,
            // Standard resolver provides primitive and common types (string, int, lists, etc.)
            MessagePack.Resolvers.StandardResolver.Instance,
            // Contractless for [MessagePackObject(keyAsPropertyName: true)] models
            MessagePack.Resolvers.ContractlessStandardResolver.Instance
        );

        var options = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
        MessagePackSerializer.DefaultOptions = options;
        Debug.Log("MessagePack初始化完成");
    }


此版本下很麻烦,已弃用,懒得写
插件Github链接

最后修改:2025 年 12 月 11 日
如果觉得我的文章对你有用,请随意赞赏