System.Serializable 无法处理 Unity 中的 List<MyClass>?

Dtb*_*b49 3 c# serialization unity-game-engine

我正在创建一个 Loot 系统。我几乎快要结束了,剩下的就是DropTable在我的Enemy脚本的检查器中填写。出于某种原因,我的DropTable脚本正在序列化,但我的LootDrop类不是。我的课程基本上是这样设置的:

DropTable 班级:

[System.Serializable]
public class DropTable
{
 public List<LootDrop> loot = new List<LootDrop>();

 public Item GetDrop()
 {
    int roll = Random.Range(0, 101);
    int chanceSum = 0;
    foreach (LootDrop drop in loot)
    {
        chanceSum += drop.Chance;
        if (roll < chanceSum)
        {
            return ItemDatabase.Instance.GetItem(drop.itemSlug); //return the item here
        }
    }
        return null; //Didn't get anything from the roll
 }
}
Run Code Online (Sandbox Code Playgroud)

LootDrop 班级:

[System.Serializable]
public class LootDrop
{
    public string itemSlug { get; set; }
    public int Chance { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

本质上,我的DropTable只是包含一个LootDrop. 但是,我无法从检查器访问LootDrop内部的各个实例List<LootDrop>。我所做的就是public DropTable在我的Enemy脚本上创建一个变量。我觉得我以前做过类似的事情,没有问题。我在这里做错了吗?我真的很想DropTable成为一个独立于我的 Enemy 的类,因为 Enemy 不应该真正关心这个GetDrop()方法。但是,如果这是唯一的方法,那么我想它必须这样做。对此事的任何帮助将不胜感激。

检查员

Lec*_*ece 5

Unity 将序列化字段,而不是属性。要么切换到字段:

[Serializable]
public class LootDrop
{
    public int Chance;
}
Run Code Online (Sandbox Code Playgroud)

或者使用序列化的支持字段:

[Serializable]
public class LootDrop
{
    public int Chance
    {
        get { return _chance; }
        set { _chance = value; }
    }

    [SerializeField]
    private int _chance;
}
Run Code Online (Sandbox Code Playgroud)