private void Start() { Item_D sword = new Item_D("sword", 0); Item_D bread = new Item_D("bread", 1); itemList.Add(sword); itemList.Add(bread); itemDictionary.Add(0, sword); itemDictionary.Add(1, bread); var dictionaryItem0 = itemDictionary[0]; // Debug.Log(dictionaryItem0.name); // Debug.Log(dictionaryItem0.id); for (int i = 0; i < itemList.Count; i++) { // Debug.Log(itemList[i].id); // Debug.Log(itemList[i].name); } for (int i = 0; i < itemDictionary.Count; i++) { // Debug.Log(itemDictionary[i].id); // Debug.Log(itemDictionary[i].name); } /* How to access using foreach */ // foreach(var item in itemDictionary) foreach (KeyValuePair <int, Item_D> item in itemDictionary) { // Debug.Log(item.Key); // Debug.Log(item.Value.id); // Debug.Log(item.Value.name); } foreach (int key in itemDictionary.Keys) { // Debug.Log(key); } foreach (Item_D item in itemDictionary.Values) { // Debug.Log("item name : " + item.name); } /* How to use ths ContainKey Method */ if (itemDictionary.ContainsKey(60)) { Debug.Log("You found the Key !!"); var randomItem = itemDictionary[60].name; } else { Debug.Log("Key dose not exist !!"); } }
void Start() { Item_D apple = new Item_D("Apple", 0); Item_D banana = new Item_D("Banana", 1); itemDB.Add(apple.id, apple); itemDB.Add(banana.id, banana); foreach (KeyValuePair <int, Item_D> item in itemDB) { Debug.Log(item.Key + " = " + item.Value.id + " : " + item.Value.name); } Debug.Log("ItemDB に登録されてる数 :" + itemDB.Count); }