2 回答

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊
這是否意味著我必須依次插入我的密鑰?
是的,您需要按順序初始化每個:
planets[Planet.CharacterId] = new Dictionary<string, Dictionary<string, int>>();
planets[Planet.CharacterId]["MetalMine"] = new Dictionary<string, int>();
planets[Planet.CharacterId]["MetalMine"]["Level"] = 0;
您可以在此處使用集合初始值設(shè)定項語法,但這不會使內(nèi)容更具可讀性和可維護(hù)性。
您似乎最好使用一個類,而不是字典的字典:
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貢獻(xiàn)1934條經(jīng)驗 獲得超2個贊
它可能有幫助或嵌套字典替代。
創(chuàng)建 Sample.cs 腳本并對其進(jìn)行測試。
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 關(guān)注
- 356 瀏覽
添加回答
舉報