카테고리 없음
2018.03.27 수업내용 (Json파싱, 데이터 바인딩)
일등하이
2018. 5. 15. 21:24
반응형
1 2 3 4 5 6 7 8 9 10 11 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System; [Serializable] public class HeroData { public int id; public string name; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; public class JsonTest : MonoBehaviour { //딕셔너리 선언 Dictionary<int, HeroData> dicHeroData; void Start () { //데이터 파일 로드 var obj = Resources.Load("hero_data") as TextAsset; //딕셔너리 생성 this.dicHeroData = new Dictionary<int, HeroData>(); //Json배열로 변환 JArray ja = JArray.Parse(obj.text); //배열의 각 데이터를 JObject로 변환 foreach (var jt in ja) { JObject jo = JObject.Parse(jt.ToString()); HeroData heroData = JsonConvert.DeserializeObject<HeroData>(jo.ToString()); this.dicHeroData.Add(heroData.id, heroData); } foreach (var pair in dicHeroData) { //Debug.Log(pair.Key + "," + pair.Value); var heroData = pair.Value; Debug.LogFormat("key: {0} value: {1}", pair.Key, pair.Value); Debug.LogFormat("id: {0} name: {1}", heroData.id, heroData.name); } Debug.LogFormat("{0} {1}", this.dicHeroData[0].id , this.dicHeroData[0].name); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class Test : MonoBehaviour { List<Item> inventory; Dictionary<int, Item> dicItem = new Dictionary<int, Item>(); //KeyValuePair<int, Item> void Start () { Item item1 = new Item(); item1.name = "단검"; item1.itemType = ItemEnum.ItemType.WEAPON; dicItem.Add(123, item1); var foundItem = dicItem[123]; var itemList = dicItem.ToList(); foreach (var item in dicItem) { Debug.Log(item.Key + " , " + item.Value); } foreach (var item in itemList) { Debug.Log(item.Key + " , " + item.Value); } inventory = new List<Item>(); Item item2 = new Item(); item2.name = "장검"; item2.itemType = ItemEnum.ItemType.WEAPON; Item item3 = new Item(); item3.name = "천옷"; item3.itemType = ItemEnum.ItemType.ARMOR; inventory.Add(item1); inventory.Add(item2); inventory.Add(item3); foreach (var item in inventory) { Debug.Log(item.name + " , " + item.itemType); } var weapons = inventory.Where(x => x.itemType == ItemEnum.ItemType.WEAPON); foreach (var weapon in weapons) { Debug.Log(weapon.name + " , " + weapon.itemType); } Debug.Log(GameConstants.VERSION); Debug.Log(ItemEnum.ItemType.WEAPON); // WEAPON Debug.Log((int)ItemEnum.ItemType.WEAPON); // 1 ItemEnum.ItemType itemType; itemType = ItemEnum.ItemType.MATERIAL; itemType = ItemEnum.ItemType.ARMOR; Debug.Log("itemType: " + itemType); Debug.Log(itemType.ToString() == "WEAPON"); var parsedItemType = System.Enum.Parse(typeof(ItemEnum.ItemType), "WEAPON"); Debug.Log("parsedItemType: " + parsedItemType); int a = 0; Debug.Log((ItemEnum.ItemType)a); // enum -> string // string -> enum // int -> enum // enum -> int // 0, 1, 2, 3, 4 .... } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Item { public string name; public ItemEnum.ItemType itemType; public virtual void Use() { //사용 하는 방법은 자식에서 재 정의 하여 결정함 //반드시 인벤토리에서 제거됨 } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Weapon : Item { public override void Use() { //Weapon은 사용할수 없습니다. } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Potion : Item { public override void Use() { //사용자의 체력을 회복 합니다. base.Use(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class ItemEnum { public enum ItemType { NONE, WEAPON, ARMOR, MATERIAL, TOOL } } | cs |
캐릭터 로드 하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Character : MonoBehaviour { [SerializeField] private string characterName; private HeroData heroData; public void SetData(HeroData heroData) { this.heroData = heroData; this.characterName = this.heroData.name; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System; [Serializable] public class HeroData { public int id; public string name; public float damage; public string assetName; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; public class JsonTest : MonoBehaviour { //딕셔너리 선언 Dictionary<int, HeroData> dicHeroData; void Start () { //데이터 파일 로드 var obj = Resources.Load("hero_data") as TextAsset; //딕셔너리 생성 this.dicHeroData = new Dictionary<int, HeroData>(); //Json배열로 변환 JArray ja = JArray.Parse(obj.text); //배열의 각 데이터를 JObject로 변환 foreach (var jt in ja) { JObject jo = JObject.Parse(jt.ToString()); HeroData heroData = JsonConvert.DeserializeObject<HeroData>(jo.ToString()); this.dicHeroData.Add(heroData.id, heroData); } Debug.LogFormat("{0} {1} {2} {3}", this.dicHeroData[0].id , this.dicHeroData[0].name, this.dicHeroData[0].damage, this.dicHeroData[0].assetName); foreach (var pair in this.dicHeroData) { //어셋 이름을 가져옴 string assetName = pair.Value.assetName; //어셋이름으로 Resources폴더에서 로드(메모리에 올림) 하고 형 변환 함 var go = Resources.Load(assetName) as GameObject; //프리팹을 만듬 var prefab = GameObject.Instantiate(go); //Character컴포넌트를 가져옴 Character character = prefab.GetComponent<Character>(); QuerySDMecanimController controller = prefab.GetComponent<QuerySDMecanimController>(); //데이터 셋팅 character.SetData(pair.Value); //애니메이션을 플레이 controller.ChangeAnimation(QuerySDMecanimController.QueryChanSDAnimationType.NORMAL_WALK); } } } |
반응형