unity SortedDictionary
时间: 2024-01-25 08:05:00 浏览: 81
unity3d-ordered-dictionary:用于将有序字典添加到自定义`ScriptableObject`和`MonoBehaviour`类的库,只要键和值类型可序列化,可通过Unity对其进行序列化
Unity does not have a SortedDictionary class built-in, but it is possible to use the System.Collections.Generic.SortedDictionary class from the .NET framework by adding the namespace to the script:
```
using System.Collections.Generic;
```
Then you can declare and use a SortedDictionary object like this:
```
SortedDictionary<string, int> myDict = new SortedDictionary<string, int>();
myDict.Add("apple", 5);
myDict.Add("banana", 3);
myDict.Add("cherry", 7);
myDict.Add("durian", 2);
foreach (KeyValuePair<string, int> kvp in myDict)
{
Debug.Log(kvp.Key + ": " + kvp.Value);
}
```
This will output:
```
apple: 5
banana: 3
cherry: 7
durian: 2
```
Note that the items are automatically sorted by the key (in this case, the string) in ascending order. To sort by a different criteria, you can provide a custom comparer to the SortedDictionary constructor.
阅读全文