2 回答
TA贡献1871条经验 获得超13个赞
这是否意味着我必须依次插入我的密钥?
是的,您需要按顺序初始化每个:
planets[Planet.CharacterId] = new Dictionary<string, Dictionary<string, int>>();
planets[Planet.CharacterId]["MetalMine"] = new Dictionary<string, int>();
planets[Planet.CharacterId]["MetalMine"]["Level"] = 0;
您可以在此处使用集合初始值设定项语法,但这不会使内容更具可读性和可维护性。
您似乎最好使用一个类,而不是字典的字典:
public class Planet
{
public List<Mine> Mines { get; set; }
}
public class Mine
{
public string Type { get; set; }
public int Level { get; set; }
}
var planets = new Dictionary<string, Planet>();
planets[Planet.CharacterId] = new Planet
{
Mines = new List<Mine>
{
new Mine
{
Type = "Metal",
Level = 0
}
};
}
TA贡献1934条经验 获得超2个赞
它可能有帮助或嵌套字典替代。
创建 Sample.cs 脚本并对其进行测试。
public Dictionary<string,Tuple<string,string,string,int>> _planets = new Dictionary<string, Tuple<string,string, string, int>>();
void Start()
{
string myKey = string.Concat("1","MetalMine","Level");
if(!_planets.ContainsKey(myKey))
{
_planets.Add(myKey,Tuple.Create("1","MetalMine","Level",0));
}
Debug.Log("_planets mykey "+myKey+" ==> "+_planets[myKey].Item4);
}
- 2 回答
- 0 关注
- 320 浏览
添加回答
举报