【问题标题】:JSON changing enums value in List of enums back to 0JSON将枚举列表中的枚举值更改回0
【发布时间】:2020-03-29 00:23:46
【问题描述】:

我有一个类,它有多个变量,这些变量都是通过 JSON 文本文件正确创建的,除了一个属于 Effects 类的枚举列表

下面的类总是将 effectedBy 列表中的枚举默认为元素 0

[System.Serializable]
public class InteractableObject : Item, IEffectReceiver
{
    protected List<Stats> stats = new List<Stats>();
    protected HashSet<Effects> currentEffects;
    public List<Effects.EffectType> effectedBy = new List<Effects.EffectType>();
    public GameObject destroyedPrefab;
    private bool destructible = true;
    private bool destroyed = false;

这是包含我要设置的枚举的类

[System.Serializable]
public class Effects 
{
    public bool usingEffect = false;
    public int effectOverTimeChance = 0;
    public EffectType effectType;
    public Stats.StatType effectedStat = Stats.StatType.health;
    public int minAmount;
    public int maxAmount;

 public enum EffectType
    {
        bleed,
        electric,
        fire,
        heal,
        physical,
        poison
    }

这是我的 JSON 文本,除了最后一个条目上的一个 enum effectedBy 之外,这一切都很好

{
  "meleeWeapons": [
    {
      "id": 0,
      "prefabID": "claw",
      "title": "claw",
      "description": "razor sharp claws",
      "slotType": 4,
      "effects": [
        {
          "effectType": 4,
          "minAmount": 2,
          "maxAmount": 4
        }
      ]
    },
    {
      "id": 1,
      "title": "hammer",
      "description": "heavy and intimidating",
      "slotType": 4
    }
  ],
  "interactableObjects": [
    {
      "id": 2,
      "prefabID": "BasicCrate",
      "title": "crate",
      "effectedBy": [
        {
          "effectType": 4
        }
      ]
    }
  ]
}

我可以通过将 effectedBy 更改为效果列表并将效果本身赋予我需要的枚举值来解决此问题,但如果可以的话,我宁愿将其保留为效果类型列表。 有人对此有任何见解吗?

【问题讨论】:

    标签: c# json unity3d enums


    【解决方案1】:
    public List<Effects.EffectType> effectedBy = new List<Effects.EffectType>();
    

    是主要问题。该代码表示​​您有一个EffectType 类型的值列表(或数组)。不幸的是,这不是 JSON 的形状:

    "effectedBy": [
        {
          "effectType": 4
        }
    ]
    

    这不是EffectType 的数组。它是一个对象数组,具有effectType 类型的EffectType 属性。

    因此你应该改用:

    public List<EffectTypeWrapper> effectedBy = new List<EffectTypeWrapper>();
    

    其中EffectTypeWrapper 定义为:

    public class EffectTypeWrapper
    {
        public EffectType effectType { get; set; }
    }
    

    以后,我强烈建议使用https://app.quicktype.io/#l=cs&r=json2csharp(例如https://app.quicktype.io?share=4jOYgCbQhD7tmaJf10Ln)。

    【讨论】:

    • 很抱歉,但我仍在努力理解为什么这不起作用。我添加了另一个变量 Effects.EffectType 类型,它按照我想要的方式工作。只是列表形式没有
    • 它不起作用@lukey_babes,因为EffectType 的数组与具有@987654333 的对象 数组相同@属性。
    【解决方案2】:

    我尝试创建一个新的 interactableObject 并设置 effectedBy 值,然后将其写入 JSON,得到以下结果。

    {
    "id": "",
    "prefabID": "",
    "title": "",
    "description": "",
    "itemPrefab": {
        "instanceID": 0
    },
    "myContainer": {
        "instanceID": 0
    },
    "health": {
        "current": 0.0,
        "minimum": 0.0,
        "maximum": 0.0,
        "rechargeRate": 0.0,
        "rechargeDelay": 0.0,
        "statType": 0
    },
    "effectedBy": [
        2
    ],
    "type": 0,
    "destroyedPrefab": {
        "instanceID": 0
    }
    

    }

    使用我的文本文件创建我的 interactableObject 并分配没有 effectType 变量的枚举,并且只编写枚举值就像我最初打算的那样工作

    "interactableObjects": [
        {
          "id": 2,
          "prefabID": "BasicCrate",
          "title": "crate",
          "effectedBy": [
            2,
            4
          ],
        }
    

    ]

    【讨论】:

    • 如果可以选择更改 JSON,最好在您的问题中更清楚地说明这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多