아카이브
[주말] LearnUGUI 데이터 연동 연습 본문
엑셀 파일을 만들어줌
csv 파일을 제이슨 코드로 변환
https://shancarter.github.io/mr-data-converter/
Mr. Data Converter
shancarter.github.io
제이슨 코드를 붙여넣기 한 후
Online JSON Viewer
jsonviewer.stack.hu
Format 누르기
맨 위 블록은 삭제한다
메모장에 복사 붙여넣기 한 후 json 파일로 저장
유니티 Resources 폴더에 json 파일을 넣고 DataManager 스크립트 만들어줌
유니티에 newtonsoft Json가 임포트 되어 있어야 함
ChestData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChestData
{
public int id;
public string name;
public int type;
public int price;
public string sprite_name;
}
Datamanager.cs
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DataManager
{
public static readonly DataManager instance = new DataManager();
private Dictionary<int, ChestData> dicChestDatas = new Dictionary<int, ChestData>();
//chest 데이터 로드 하기
public void LoadChestData()
{
//Resources 폴더에서 chest_data 에셋 (TextAsset) 을 로드
TextAsset asset = Resources.Load<TextAsset>("chest_data");
//json 문자열 가져오기
string json = asset.text;
Debug.Log(json);
//역직렬화
ChestData[] arrChestDatas = JsonConvert.DeserializeObject<ChestData[]>(json);
Debug.LogFormat("arrChestDatas.Length: {0}", arrChestDatas.Length);
//사전에 넣기
this.dicChestDatas = arrChestDatas.ToDictionary(x => x.id); //배열에 있는 요소로 새로운 사전을 만듬
Debug.LogFormat("this.dicChestDatas.Count: {0}", this.dicChestDatas.Count);
}
public List<ChestData> GetChestData()
{
return this.dicChestDatas.Values.ToList();
}
}
UIMain.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIMain : MonoBehaviour
{
void Start()
{
DataManager.instance.LoadChestData();
}
}
'유니티 심화' 카테고리의 다른 글
[LearnUGUI] 애니메이션 연출 (0) | 2023.09.12 |
---|---|
[LearnUGUI] Input System (0) | 2023.09.05 |
LearnUGUI (0) | 2023.09.04 |
[주말] Input System 연습 (0) | 2023.09.04 |
[SaveAndLoad] 데이터 연동 (0) | 2023.09.01 |