unity3d Dictionary 根据key获取value
using System; using System.Collections.Generic; using UnityEngine; public class DictionaryExample : MonoBehaviour { private Dictionary<string, int> myDictionary; void Start() { // 初始化字典并添加一些键值对 myDictionary = new Dictionary<string, int>(); myDictionary.Add("One", 1); myDictionary.Add("Two", 2); myDictionary.Add("Three", 3); // 根据键获取值 string keyToSearch = "Two"; if (myDictionary.ContainsKey(keyToSearch)) { int value = myDictionary[keyToSearch]; Debug.Log("The value for key '" + keyToSearch + "' is: " + value); } else { Debug.Log("The key '" + keyToSearch + "' does not exist in the dictionary."); } } }
###########################
QQ 3087438119