2019-11-07 Unity
Unity/실습 2019. 11. 7. 17:51-
using System.Collections;
-
using System.Collections.Generic;
-
using Newtonsoft.Json;
-
using UnityEngine;
-
-
public class App : MonoBehaviour
-
{
-
private Hero hero;
-
// Start is called before the first frame update
-
void Start()
-
{
-
//텍스트 리소스를 로드한다
-
TextAsset asset = Resources.Load<TextAsset>("Data/Character_Data");
-
//캐릭터데이터를 만들고 테이블과 똑같이 세팅;
-
//캐릭터데이터에 리소스를 역직렬화
-
CharacterData[] arrCharacterData=JsonConvert.DeserializeObject<CharacterData[]>(asset.text);
-
//딕셔너리 key와 밸류(캐릭터데이터)를 생성한다
-
Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
-
//반복문으로 데이터를 확인한다
-
foreach(var data in arrCharacterData)
-
{
-
Debug.LogFormat("{0} {1} {2} {3} {4}", data.id, data.name, data.prefab_name, data.hp, data.damage);
-
//딕셔너리 데이터id,데이터를 생성한다
-
dicCharacterData.Add(data.id, data);
-
}
-
//검색한 아이디를 변수에 담는다.
-
var characterData1 = dicCharacterData[1000];
-
var characterData2 = dicCharacterData[2000];
-
//인포 클래스를 만들고 바뀔수있는 값을 클래스에 저장
-
CharacterInfo info = new CharacterInfo(characterData1.id, characterData1.hp, characterData1.damage);
-
CharacterInfo info2 = new CharacterInfo(characterData2.id, characterData2.hp, characterData2.damage);
-
//모델을 게임오브젝트 타입으로 불러온다.(리소스로드)
-
string path = string.Format("Model/{0}", characterData1.prefab_name);
-
var model = Resources.Load<GameObject>(path);
-
-
string path2 = string.Format("Model/{0}", characterData2.prefab_name);
-
var model2 = Resources.Load<GameObject>(path2);
-
//모델을 인스턴스화한뒤 변수에 담는다(prefab)
-
var prefab = Instantiate(model);
-
var prefab2 = Instantiate(model2);
-
//게임오브젝트를 생성하고 이름을 정해준다
-
GameObject heroGo = new GameObject(characterData1.name);
-
GameObject heroGo2 = new GameObject(characterData2.name);
-
//프리팹의 부모설정,좌표지정
-
prefab.transform.SetParent(heroGo.transform);
-
prefab.transform.localPosition = Vector3.zero;
-
-
prefab2.transform.SetParent(heroGo2.transform);
-
prefab2.transform.localPosition = Vector3.zero;
-
//게임오브젝트에 클래스를 컴포넌트해준다
-
var hero = heroGo.AddComponent<Hero>();
-
var hero2 = heroGo2.AddComponent<Hero>();
-
//변수에 모델,정보,좌표를 정해준다.
-
hero.InIt(model,info,Vector3.zero);
-
hero2.InIt(model2, info2, new Vector3(1, 0, 1));
-
-
-
}
-
-
// Update is called once per frame
-
void Update()
-
{
-
-
}
-
}
-
using System.Collections;
-
using System.Collections.Generic;
-
using UnityEngine;
-
-
public class CharacterData
-
{
-
public int id;
-
public string name;
-
public string prefab_name;
-
public int hp;
-
public int damage;
-
}
-
using System.Collections;
-
using System.Collections.Generic;
-
using UnityEngine;
-
-
public class CharacterInfo
-
{
-
public int id;
-
public int hp;
-
public int damage;
-
public CharacterInfo(int id,int hp,int damage)
-
{
-
this.id = id;
-
this.hp = hp;
-
this.damage = damage;
-
}
-
}
-
using System.Collections;
-
using System.Collections.Generic;
-
using UnityEngine;
-
-
public class Hero : MonoBehaviour
-
{
-
private GameObject model;
-
private CharacterInfo info;
-
// Start is called before the first frame update
-
void Start()
-
{
-
-
}
-
-
// Update is called once per frame
-
void Update()
-
{
-
-
}
-
public void InIt(GameObject model,CharacterInfo info,Vector3 post)
-
{
-
this.info = info;
-
this.model = model;
-
this.transform.position = post;
-
}
-
}
'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 |