회원 로그인
|
Dictionary (딕셔너리) (with Unity)
C# Dictionary (딕셔너리)
-키(Key)와 값(Value)을 넣어 키로 검색으로 값을 얻어내는 이점이 있다.
-db, 서버 통신에서 읽기/저장 할 때 사용
-json 사용
1. 아이템 추가 : Dictionary.Add(아이템)
2. 아이템 검색 : Dictionary["키이름"]
3. 아이템 삭제 : Dictionary.Remove("키이름")
4. 아이템 존재 유무 검사 : Dictionary.ContainsKey("키이름")
-키(Key)와 값(Value)을 넣어 키로 검색으로 값을 얻어내는 이점이 있다.
-db, 서버 통신에서 읽기/저장 할 때 사용
-json 사용
1. 아이템 추가 : Dictionary.Add(아이템)
2. 아이템 검색 : Dictionary["키이름"]
3. 아이템 삭제 : Dictionary.Remove("키이름")
4. 아이템 존재 유무 검사 : Dictionary.ContainsKey("키이름")
using UnityEngine; using System.Collections; using System.Collections.Generic; public class dicStudy1 : MonoBehaviour { void Start () { Dictionary<string, string> dt = new Dictionary<string, string>(); dt["Apple"] = "사과"; dt["Banana"] = "바나나"; dt["Orange"] = "오렌지"; Debug.Log("Apple : " + dt["Apple"]); Debug.Log("Banana : " + dt["Banana"]); Debug.Log("Orange : " + dt["Orange"]); } // Update is called once per frame void Update () { } }
using UnityEngine; using System.Collections; using System.Collections.Generic; public class Item { string itemName; int str; int dex; int hp; public Item(string itemName, int str, int dex, int hp) { this.itemName = itemName; this.str = str; this.dex = dex; this.hp = hp; } public void ShowItemStat() { Debug.Log(this.itemName + ", STR(" + this.str + "), DEX(" + this.dex + "), HP(" + this.hp + ")"); } } public class dicStudy : MonoBehaviour { Dictionary<string, Item> itemMap; void Start () { // 제네릭 타입 string(Key), Item(Value) 2개 사용 itemMap = new Dictionary<string, Item>(); string itemName; // 아이템 추가 itemName = "영혼의 검"; itemMap.Add(itemName, new Item(itemName, 100, 80, 80)); // itemMap["영혼의 검"] = new Item(itemName,100,80,80); // 리스트 itemName = "운둔자의 방패"; itemMap.Add(itemName, new Item(itemName, 30, 30, 50)); itemName = "혼돈의 망토"; itemMap.Add(itemName, new Item(itemName, 10, 80, 50)); itemName = "도적의 단검"; itemMap.Add(itemName, new Item(itemName, 60, 90, 40)); // 아이템 검색 // .ContainsKey 키를 찾아 true/false로 리턴 if(itemMap.ContainsKey("혼돈의 망토")) { // 바로 사용할 때 null이면 오류 그래서 .ContainsKey로 조건을 닮 Item item = itemMap["혼돈의 망토"]; item.ShowItemStat(); } Debug.Log("Dictionary Count : " + itemMap.Count); // 아이템 목록 출력 (while) // var enumerator = itemMap.GetEnumerator(); // var형 - 모든 타입을 지향 while (enumerator.MoveNext()) // .MoveNext() - 다음 키로 넘어감 true/false로 리턴 { var pair = enumerator.Current; // .Current - 지정한 키와 값을 꺼내기 Item item = pair.Value; // .Value - pair의 값을 item 변수에 대입 item.ShowItemStat(); // } // 키이름의 아이템을 삭제 bool result = itemMap.Remove("혼돈의 망토"); if (result) { Debug.Log("혼돈의 망토 아이템이 삭제됨"); } // 아이템 목록 출력 (for) foreach(KeyValuePair<string, Item> pair in itemMap) // 꺼내기 { Item item = pair.Value; item.ShowItemStat(); } // 딕셔너리 모두 삭제 itemMap.Clear(); } }
- dictionary.unitypackage (5.8KB) (0)
댓글 3개
| 엮인글 0개
285개(6/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
185 | [PHP] 회원가입 + 로그인 스크립트 (with Unity) [2+1] | 푸딩뱃살 | 12240 | 2015.12.06 17:31 |
184 | [C#] C# 추천 서적 | 푸딩뱃살 | 1322 | 2015.12.06 17:16 |
183 | [C/C++] C++ 참고 사이트 | 푸딩뱃살 | 1182 | 2015.12.04 16:13 |
182 | [PHP] 클래스 | 푸딩뱃살 | 3581 | 2015.12.04 14:40 |
181 | [PHP] CodeIgniter(코드이그나이트) | 푸딩뱃살 | 2932 | 2015.12.04 14:40 |
180 | [C#] Delegate (델리게이트) (with Unity) | 푸딩뱃살 | 6267 | 2015.12.01 10:44 |
179 | [PHP] php 함수 | 푸딩뱃살 | 2219 | 2015.11.30 15:33 |
178 | [PHP] Dictionary (딕셔너리) | 푸딩뱃살 | 2869 | 2015.11.27 12:37 |
177 | [PHP] 배열 | 푸딩뱃살 | 2170 | 2015.11.27 12:37 |
176 | [PHP] 변수 선언 / 산술 연산 | 푸딩뱃살 | 2183 | 2015.11.27 11:14 |
>> | [C#] Dictionary (딕셔너리) (with Unity) [3+3] | 푸딩뱃살 | 14484 | 2015.11.25 10:29 |
174 | [C#] List (리스트) (with Unity) | 푸딩뱃살 | 12088 | 2015.11.24 10:22 |
173 | [C#] Generic (제네릭) (with Unity) | 푸딩뱃살 | 4889 | 2015.11.22 12:32 |
172 | [C/C++] Templete (템플릿) | 푸딩뱃살 | 2046 | 2015.11.22 12:15 |
171 | [C#] 키보드 입력 | 푸딩뱃살 | 3570 | 2015.11.21 18:00 |
170 | [C#] 예외와 예외 처리 | 푸딩뱃살 | 4124 | 2015.11.21 17:39 |
169 | [C#] Struct (구조체) (with Unity) | 푸딩뱃살 | 5124 | 2015.11.18 22:08 |
168 | [C#] Property (프로퍼티) (with Unity) | 푸딩뱃살 | 2444 | 2015.11.18 10:50 |
167 | [C#] interface (인터페이스) (with Unity) | 푸딩뱃살 | 3308 | 2015.11.15 17:32 |
166 | [C#] 스트림 (stream) - 문자, 바이너리 읽기/쓰기 | 푸딩뱃살 | 12780 | 2015.11.14 18:24 |