2019-11-07 Unity

Unity/실습 2019. 11. 7. 17:51

  1. using System.Collections;

  2. using System.Collections.Generic;

  3. using Newtonsoft.Json;

  4. using UnityEngine;

  5.  

  6. public class App : MonoBehaviour

  7. {

  8.     private Hero hero;

  9.     // Start is called before the first frame update

  10.     void Start()

  11.     {

  12.         //텍스트 리소스를 로드한다

  13.         TextAsset asset = Resources.Load<TextAsset>("Data/Character_Data");

  14.         //캐릭터데이터를 만들고 테이블과 똑같이 세팅;

  15.         //캐릭터데이터에 리소스를 역직렬화

  16.         CharacterData[] arrCharacterData=JsonConvert.DeserializeObject<CharacterData[]>(asset.text);

  17.         //딕셔너리 key와 밸류(캐릭터데이터)를 생성한다

  18.         Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();

  19.         //반복문으로 데이터를 확인한다

  20.         foreach(var data in arrCharacterData)

  21.         {

  22.             Debug.LogFormat("{0} {1} {2} {3} {4}", data.id, data.name, data.prefab_name, data.hp, data.damage);

  23.             //딕셔너리 데이터id,데이터를 생성한다

  24.             dicCharacterData.Add(data.id, data);

  25.         }

  26.         //검색한 아이디를 변수에 담는다.

  27.         var characterData1 = dicCharacterData[1000];

  28.         var characterData2 = dicCharacterData[2000];

  29.         //인포 클래스를 만들고 바뀔수있는 값을 클래스에 저장

  30.         CharacterInfo info = new CharacterInfo(characterData1.id, characterData1.hp, characterData1.damage);

  31.         CharacterInfo info2 = new CharacterInfo(characterData2.id, characterData2.hp, characterData2.damage);

  32.         //모델을 게임오브젝트 타입으로 불러온다.(리소스로드)

  33.         string path = string.Format("Model/{0}", characterData1.prefab_name);

  34.         var model = Resources.Load<GameObject>(path);

  35.  

  36.         string path2 = string.Format("Model/{0}", characterData2.prefab_name);

  37.         var model2 = Resources.Load<GameObject>(path2);

  38.         //모델을 인스턴스화한뒤 변수에 담는다(prefab)

  39.         var prefab = Instantiate(model);

  40.         var prefab2 = Instantiate(model2);

  41.         //게임오브젝트를 생성하고 이름을 정해준다

  42.         GameObject heroGo = new GameObject(characterData1.name);

  43.         GameObject heroGo2 = new GameObject(characterData2.name);

  44.         //프리팹의 부모설정,좌표지정

  45.         prefab.transform.SetParent(heroGo.transform);

  46.         prefab.transform.localPosition = Vector3.zero;

  47.  

  48.         prefab2.transform.SetParent(heroGo2.transform);

  49.         prefab2.transform.localPosition = Vector3.zero;

  50.         //게임오브젝트에 클래스를 컴포넌트해준다

  51.         var hero = heroGo.AddComponent<Hero>();

  52.         var hero2 = heroGo2.AddComponent<Hero>();

  53.         //변수에 모델,정보,좌표를 정해준다.

  54.         hero.InIt(model,info,Vector3.zero);

  55.         hero2.InIt(model2, info2, new Vector3(1, 0, 1));

  56.  

  57.  

  58.     }

  59.  

  60.     // Update is called once per frame

  61.     void Update()

  62.     {

  63.        

  64.     }

  65. }

  1. using System.Collections;

  2. using System.Collections.Generic;

  3. using UnityEngine;

  4.  

  5. public class CharacterData

  6. {

  7.     public int id;

  8.     public string name;

  9.     public string prefab_name;

  10.     public int hp;

  11.     public int damage;

  12. }

  1. using System.Collections;

  2. using System.Collections.Generic;

  3. using UnityEngine;

  4.  

  5. public class CharacterInfo

  6. {

  7.     public int id;

  8.     public int hp;

  9.     public int damage;

  10.     public CharacterInfo(int id,int hp,int damage)

  11.     {

  12.         this.id = id;

  13.         this.hp = hp;

  14.         this.damage = damage;

  15.     }

  16. }

  1. using System.Collections;

  2. using System.Collections.Generic;

  3. using UnityEngine;

  4.  

  5. public class Hero : MonoBehaviour

  6. {

  7.     private GameObject model;

  8.     private CharacterInfo info;

  9.     // Start is called before the first frame update

  10.     void Start()

  11.     {

  12.        

  13.     }

  14.  

  15.     // Update is called once per frame

  16.     void Update()

  17.     {

  18.  

  19.     }

  20.     public void InIt(GameObject model,CharacterInfo info,Vector3 post)

  21.     {

  22.         this.info = info;

  23.         this.model = model;

  24.         this.transform.position = post;

  25.     }

  26. }

'Unity > 실습' 카테고리의 다른 글

2019_12_18 UI:(2)  (0) 2019.12.18
2019_12_18 UI  (0) 2019.12.18
2019-11-13 Unity 버튼을 눌러 캐릭터 생성/메서드  (0) 2019.11.13