Introduction to Dictionary

Introduction to Dictionary

Dictionary is a generic collection class in C# for storing key-value pairs. In Unity, Dictionary is often used to cache resources in games, manage game objects, etc. Dictionary provides a method to quickly find key-value pairs, which can improve code execution efficiency.

Dictionary method

The following are some commonly used methods in the Dictionary class:

Dictionary.Add()

Used to add a key-value pair to the dictionary, the parameters are key and value.

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);

Dictionary.ContainsKey()

Determine whether the specified key is contained in the dictionary, the parameter is the key.

Dictionary<string, int> dict = new Dictionary<string, int>();
if (dict.ContainsKey("apple"))
{
    
    
    int value = dict["apple"];
    Debug.Log(value);
}

Dictionary.ContainsValue()

Determines whether the specified value is contained in the dictionary, the parameter is the value.

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);
if (dict.ContainsValue(1))
{
    
    
    Debug.Log("Found value = 1");
}

Dictionary.TryGetValue()

Attempts to get the value associated with the specified key, returning true if the key is found and storing the value in the output parameter; otherwise returning false.

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);
int value;
if (dict.TryGetValue("apple", out value))
{
    
    
    Debug.Log(value);
}

Dictionary.Remove()

Remove the key-value pair of the specified key from the dictionary, the parameter is the key.

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);
dict.Remove("apple");

Dictionary example

Here are some common examples of using Dictionary in Unity:

Cache resources

Dictionary<string, AudioClip> cache = new Dictionary<string, AudioClip>();
AudioClip clip = Resources.Load<AudioClip>("path/to/audio");
cache.Add("path/to/audio", clip);

In games, there are usually many resources that need to be loaded, and these resources are usually relatively large. If you have to reload each time you use them, it will have a great impact on game performance. Therefore, using Dictionary to cache resources can help us improve the game. performance.

Manage game objects

Dictionary<int, GameObject> objects = new Dictionary<int, GameObject>();
GameObject go = GameObject.Instantiate(prefab);
objects.Add(go.GetInstanceID(), go);

In games, there are usually a large number of game objects. If we need to operate on a certain game object, we need to traverse the entire scene or hierarchical structure, which is very inefficient. Using Dictionary can help us quickly find the specified game object and improve code execution efficiency.

store player data

Dictionary<string, int> playerData = new Dictionary<string, int>();
playerData.Add("level", 10);
playerData.Add("exp", 100);

In games, it is usually necessary to store player data, such as levels, experience points, and other information. These data can be conveniently stored and accessed using Dictionary.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/131154837
Recommended