
유니티 Json 데이터 관리Unity/Study2023. 12. 12. 21:11
Table of Contents
기능 소개
유니티에는 여러 데이터 저장 방식 중, Json으로 데이터를 저장하는 기능을 구현하도록 한다
기능 구현
DataManager.cs
public class DataManager
{
public Dictionary<string, ItemData> Items = new();
public void Initialize()
{
Items = LoadJson<ItemDataLoader, string, ItemData>("ItemData").MakeData();
}
private TLoader LoadJson<TLoader, Tkey, TValue>(string path) where TLoader : ILoadData<Tkey, TValue>
{
TextAsset textAsset = Main.Resource.Load<TextAsset>(path);
return JsonConvert.DeserializeObject<TLoader>(textAsset.text);
}
}
- DataManager 클래스를 생성하여 Dictionary에서 Json 파일에 할당된 string key값에 따라 해당 데이터를 불러오도록 한다
ItemData.cs
[Serializable]
public class ItemData
{
public ItemType itemType;
public string itemStringKey;
public int itemStatHP;
public int itemStatATK;
public int itemStatDEF;
public float itemStatCRIT;
public string itemName;
public string itemDesc;
public int itemPrice;
}
[Serializable]
public class ItemDataLoader : ILoadData<string, ItemData>
{
public List<ItemData> items = new();
public Dictionary<string, ItemData> MakeData()
{
return items.ToDictionary(item => item.itemStringKey);
}
}
- Json에 할당된 값을 똑같이 ItemData 클래스에 생성된 필드 값에 로드되도록 ItemDataLoader를 통해 데이터를 파싱한다
'Unity > Study' 카테고리의 다른 글
유니티 로그라이크 문으로 방 연결하기 (0) | 2023.12.18 |
---|---|
유니티 로그라이크 방 생성 (0) | 2023.12.15 |
유니티 Stat Modifier (0) | 2023.12.11 |
유니티 Manager System (0) | 2023.12.05 |
유니티 2D 공 충돌 물리엔진 (0) | 2023.11.30 |