Unity 字典存入List怎么操作
时间: 2024-05-04 11:21:21 浏览: 229
在 Unity 中,可以将 Dictionary 存入 List 中,具体操作如下:
1. 定义一个 Dictionary 对象 dict 和一个 List 对象 list:
```
Dictionary<string, int> dict = new Dictionary<string, int>();
List<Dictionary<string, int>> list = new List<Dictionary<string, int>>();
```
2. 向 dict 中添加键值对:
```
dict.Add("key1", 1);
dict.Add("key2", 2);
dict.Add("key3", 3);
```
3. 将 dict 添加到 list 中:
```
list.Add(dict);
```
4. 可以通过遍历 list 中的每个 Dictionary 对象,来访问里面的键值对:
```
foreach (Dictionary<string, int> d in list)
{
foreach (KeyValuePair<string, int> pair in d)
{
Debug.Log("key: " + pair.Key + ", value: " + pair.Value);
}
}
```
这样就可以将多个 Dictionary 存入一个 List 中,并且可以通过循环遍历访问其中的键值对。
阅读全文
相关推荐










