项目未添加到列表中

时间:2017-08-26 20:45:48

标签: c# unity5

我有自定义类Map,其中包含2个自定义类BricksDangers列表。问题是我的代码没有向自定义类添加元素。

public class Map
{
    public string mapName;
    public List<Brick> bricks;
    public List<Danger> dangers;

    public Map()
    {
        bricks = new List<Brick>();
        dangers = new List<Danger>();
    }
}

public class Brick
{
    public Vector2 Position;
}

public class Danger
{
    public Vector2 Position;
}
private void CreateMap()
{
    Map map = new Map();
    map.mapName = "test";

    map.bricks.Add(new Brick { Position = new Vector2(1, 1) });
    map.bricks.Add(new Brick { Position = new Vector2(2, 1) });
    map.bricks.Add(new Brick { Position = new Vector2(5, 1) });
    map.bricks.Add(new Brick { Position = new Vector2(6, 1) });
    map.bricks.Add(new Brick { Position = new Vector2(7, 1) });

    map.dangers.Add(new Danger { Position = new Vector2(3, 1) });
    map.dangers.Add(new Danger { Position = new Vector2(4, 1) });

    Debug.Log(JsonUtility.ToJson(map));
    AddMap(map);
}

作为Debug.Log的输出,我得到{"mapName:": "test"}

1 个答案:

答案 0 :(得分:0)

我认为它适用于序列化对象的类型。 将[Serializable]属性添加到您的类中,然后重试。我想你也必须把它添加到Vector属性中。