【发布时间】:2026-02-01 07:40:01
【问题描述】:
我想存储我的玩家分数。我会有不同的世界,世界有不同的层次。 这就是为什么我想要类似的东西..
public static void AddScore(int world, int level, int rolls, float time, float score)
{
_scores[world][level] = new LevelScore(rolls, time, score);
}
public static LevelScore GetScore(int world, int level)
{
if (_scores.ContainsKey(world))
{
var scoresOfWorld = _scores[world];
if(scoresOfWorld.ContainsKey(level))
{
return scoresOfWorld[level];
}
}
return new LevelScore();
}
我用字典里面的字典试过了..
public static Dictionary<int, Dictionary<int, LevelScore>> _scores = new Dictionary<int, Dictionary<int, LevelScore>>();
但是 AddScore(...) 导致“KeyNotFoundException:给定的键不在字典中”。 我认为如果密钥不存在,则会添加密钥。对我来说,轻松归档我想要的内容的最佳方式是什么?
【问题讨论】:
-
使用元组作为单个字典的键可能更容易:
Dictionary<(int World, int Level), LevelScore>。 -
这解决了我的问题!谢谢你:)
标签: c# visual-studio unity3d