我有一个简单的类来定义房间。最初,我设置了所有我需要的房间,(可能是数百个长列表),虽然在我的例子中我只设置了3个。然后我有一个字符串,我将用它来引用Rooms
类的正确实例。例如,这可能是“X10Y10”。我想使用该字符串来标识相应的Rooms
实例,但不知道如何关联它们。使用字符串变量来标识类的相应实例
void Start() {
Rooms X10Y10 = new Rooms();
X10Y10.Name = "The Great Room";
X10Y10.RoomMonsters = 10;
X10Y10.Ref = "001";
Rooms X11Y10 = new Rooms();
X11Y10.Name = "Smoking room";
X11Y10.RoomMonsters = 2;
X11Y10.Ref = "002";
Rooms X12Y10 = new Rooms();
X12Y10.Name = "Hunting Room";
X12Y10.RoomMonsters = 7;
X12Y10.Ref = "003";
// Don't Know the room Ref until runtime, during game.
// Want to get the room instance properties of one of the rooms eg.
string RoomAtRuntime = "X11Y10"; // dont know this until game is running
// fix following lines
print(RoomAtRuntime.RoomMonster); // would return 2
print(RoomAtRuntime.Name); // would return Smoking room
}
public class Rooms
{
public string Ref { get; set; }
public string Name { get; set; }
public int RoomMonsters { get; set; }
}
而不是标记'[[Resolved]]'(这里不是“一件事物”),如果您可以接受现有的答案,或者如果实际解决方案明显不同,并将*标记为接受的答案 –