데이터 테이블을 로드하고 플레이어 생성하기
데이터를 로드할 Main씬
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using UnityEngine.SceneManagement;
public class TestcreateHeroFromDataMain : MonoBehaviour
{
public Dictionary<int, HeroData> dicHeroDatas = new Dictionary<int, HeroData>();
public Dictionary<int, GameObject> dicHeroPrefabs = new Dictionary<int, GameObject>();
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
void Start()
{
this.LoadDatas();
this.LoadPrefabs();
/*************** 데이터 및 리소스 로드 완료 ***************/
SceneManager.LoadScene("TestGameScene");
}
private void LoadPrefabs()
{
foreach(KeyValuePair<int, HeroData> pair in this.dicHeroDatas)
{
HeroData heroData = pair.Value;
//경로설정
string path = string.Format("Prefabs/Hero/{0}", heroData.prefab_name);
//Debug.LogError(path);
GameObject prefab = Resources.Load<GameObject>(path);
//리소스 폴더에 바로 있다면
//GameObject prefab = Resources.Load<GameObject>(heroData.prefab_name);
//Debug.LogError(prefab);
this.dicHeroPrefabs.Add(heroData.id, prefab);
}
}
private void LoadDatas()
{
TextAsset textAsset = Resources.Load<TextAsset>("hero_data");
string json = textAsset.text;
HeroData[] arrHeroDatas = JsonConvert.DeserializeObject<HeroData[]>(json);
Debug.Log(arrHeroDatas.Length);
for (int i = 0; i < arrHeroDatas.Length; i++)
{
HeroData heroData = arrHeroDatas[i];
//Debug.LogErrorFormat("{0}, {1}, {2}, {3}", heroData.id, heroData.prefab_name, heroData.damage, heroData.hp);
this.dicHeroDatas.Add(heroData.id, heroData);
}
}
}
데이터 테이블을 저장할 Data스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroData
{
//id prefab_name damage hp
//int string float float
public int id;
public string prefab_name;
public float damage;
public float hp;
}
Main에서 데이터 로드가 끝나면 불러들일 Game씬
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestGameSceneMain : MonoBehaviour
{
TestcreateHeroFromDataMain testCreateHeroFromDataMain;
private const int DEFAULT_HERO_ID = 100;
void Start()
{
this.testCreateHeroFromDataMain = GameObject.FindObjectOfType<TestcreateHeroFromDataMain>();
CreateHero(DEFAULT_HERO_ID);
}
private void CreateHero(int id)
{
HeroData heroData = this.testCreateHeroFromDataMain.dicHeroDatas[id];
HeroInfo heroInfo = new HeroInfo(heroData.id, heroData.damage, heroData.hp);
GameObject heroPrefab = this.testCreateHeroFromDataMain.dicHeroPrefabs[heroInfo.id];
GameObject heroModel = Instantiate<GameObject>(heroPrefab);
GameObject heroGo = new GameObject();
heroGo.name = "Hero";
heroModel.transform.SetParent(heroGo.transform);
}
}
로드된 데이터를 저장할 Info
-데이터 파일로 플레이어를 생성하면 게임 시작시 데이터가 기본 데이터로 초기화된다.
-게임 진행사항(플레이어의 성장)을 저장알 Info가 필요
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroInfo
{
public int id;
public float damage;
public float hp;
public HeroInfo(int id, float damage, float hp)
{
this.id = id;
this.damage = damage;
this.hp = hp;
}
}
데이터 테이블(엑셀) 작성 -> 제이슨 언어로 변환 -> 변환된 언어를 메모장에 복사하고 Json 파일로 저장 ->
Main에서 데이터를 로드한다 -> 로딩이 완료되면 게임씬으로 넘어간다 -> 로드된 데이터를 기반으로 플레이어를 생성한다.
데이터를 가지는 오브젝트는 쉘(Shell)구조로 되어있다.
Info를 토대로
Shell 하위에 Model을 불러오고, 스크립트에 데이터를 할당한다.
'Unity > 게임 엔진 응용 프로그래밍' 카테고리의 다른 글
| [Cinemachine] 시네머신 카메라 구성, Follow Cam 만들기 (0) | 2021.11.30 |
|---|---|
| [Unity 3D] AppleCatch : 씬전환, 싱글턴 패턴 (0) | 2021.10.26 |
| [Unity 3D] MiniRPG : 랜덤한 위치에 몬스터 생성 (0) | 2021.10.22 |
| [Unity 3D] MiniRPG : 몬스터와 전투 (0) | 2021.10.20 |
| [Unity 3D] MiniRPG : 캐릭터 공격 애니메이션 (0) | 2021.10.19 |