为什么不是我的所有结构都被序列化成MessagePack?

huangapple go评论68阅读模式
英文:

Why doesn't all of my struct get serialized using MessagePack?

问题

我正在使用Unity与MessagePack保存我的游戏数据。

这是存储所有数据的结构体:

[MessagePackObject]
public struct Data {
    [Key(0)]
    public Level[] levels;
    [Key(1)]
    public float menuContentScroll;
    [Key(2)]
    public bool musicOn;
    [Key(3)]
    public int worldHighScore;
    [Key(4)]
    public bool tiltControlsEnabled;
    [Key(5)]
    public bool autoRestartEnabled;
}

这是关卡结构体:

[MessagePackObject]
public struct Level {
    [Key(0)]
    public int length;
    [Key(1)]
    public bool completed;
    [Key(2)]
    public int highScore;
    [Key(3)]
    public int attempts;
}

一切都一直正常工作,直到我决定向数据结构中添加两个布尔值,tiltControlsEnabled和autoRestartEnabled。现在这两个值无法被序列化并存储到磁盘,因此每次启动游戏时都会重置,不像musicOn这样的值,它可以被存储。

我尝试通过记录序列化后的字节数组的JSON版本到控制台,清楚地看到这两个成员不在其中。但当我尝试使用Unity的JsonUtility时,一切都正常工作。

英文:

I am using MessagePack with Unity to save data of my game.

This is the struct where all the data is stored:

[MessagePackObject]
public struct Data {
    [Key(0)]
    public Level[] levels;
    [Key(1)]
    public float menuContentScroll;
    [Key(2)]
    public bool musicOn;
    [Key(3)]
    public int worldHighScore;
    [Key(4)]
    public bool tiltControlsEnabled;
    [Key(5)]
    public bool autoRestartEnabled;
}

Here is the level struct:

[MessagePackObject]
public struct Level {
    [Key(0)]
    public int length;
    [Key(1)]
    public bool completed;
    [Key(2)]
    public int highScore;
    [Key(3)]
    public int attempts;
}

Everything have been working good unitil now when I decided to add two booleans to the data struct, tiltControlsEnabled and autoRestartEnabled. Now these two do not get serialized and stored to disk and are therefore reset every time you start the game, unlike for example musicOn which is being stored.

I experimented by logging to the console the json version of the serialized byte[] and there I saw clearly that the two members are not there. When I instead tried using Unity's JsonUtility everything worked fine.

答案1

得分: 0

我解决了。我不得不重新生成MessagePackGenerated.cs,因为新的值不在那里。

英文:

I solutioned. I had to regenerate MessagePackGenerated.cs as the new values were not there.

huangapple
  • 本文由 发表于 2023年4月17日 01:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定