我有一个类来定义例如一艘船,然后我有一个具有一些默认参数的类数组但是稍后我希望能够使用预定义的示例之一添加到类列表中。我不知道如何将预定义类添加到列表中,同时创建它的副本而不是指向原始数组的指针。这是希望使一切清楚的代码。
public enum ShipType
{
Capital,
Cruiser,
Frigate,
Fighter,
Colony,
NumShipTypes
}
public class Ship
{
public Vector3 ShipPosition;
public ShipType Type;
public string DesignName;
public Ship(ShipType type,string designame)
{
Type = type;
DesignName = designame;
ShipPosition = Vector3.zero;
}
}
然后是我的默认类型数组:-
public Ship[] AllShips = new Ship[(int)ShipType.NumShipTypes]
{
new Ship(ShipType.Capital,"MassiveOne"),
new Ship(ShipType.Cruiser,"MediumOne"),
new Ship(ShipType.Frigate,"SmallerOne"),
new Ship(ShipType.Fighter,"TinyOne"),
new Ship(ShipType.Colony,"Mayflower")
};
现在,我创建了一个列表,并在此处添加了上述内容之一的副本:-
public List<Ship> FleetShips = new List<Ship>();
FleetShips.Add(AllShips[(int)ShipType.Fighter]);
但这似乎只是在数组中添加一个引用或指针。我需要的是这样的东西,但这确实有效: -
FleetShips.Add(new AllShips[(int)ShipType.Fighter]());
但这给了我“是一个字段,但像类型一样使用”错误
有任何想法吗?