1. 실행 이미지

 

2. chest_data.xlsx

 

3. chest_data.json

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class DataManagers
{

    public static readonly DataManagers Instance = new DataManagers();
    public Dictionary<int, ChestData> dicChestDatas = new Dictionary<int, ChestData>();

   private DataManagers() { }

    public void LoadChestData()
    {
        var asset = Resources.Load<TextAsset>("chest_data");
        var json = asset.text;

        var chestDatas = JsonConvert.DeserializeObject<ChestData[]>(json);
     

        foreach (var chestData in chestDatas)
        {
            this.dicChestDatas.Add(chestData.id, chestData);
        }
        Debug.LogFormat("chest_data Load Complete! => Count : {0}", this.dicChestDatas.Count);


    }
}

4. DataManagers.cs (데이터를 역직렬화 할 메서드와 불러온 데이터를 저장 할 딕셔너리)

 

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public class TestChestMain : MonoBehaviour
{
    [SerializeField] private UIChestScrollView scrollView;
    

    private void Awake()
    {
        DataManagers.Instance.LoadChestData(); 

    }



}

5. TestChestMain.cs (제일 먼저 [Awake] DataManager를 통한 데이터 로드)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class UIChestScrollView : MonoBehaviour
{
    [SerializeField] private ChestCellView[] cells;
    [SerializeField] private GameObject chestPrefab;
    [SerializeField] private GameObject contents;
    [SerializeField] private SpriteAtlas atlas;

    void Start()
    {
        cells = new ChestCellView[DataManagers.Instance.dicChestDatas.Count];

        for (int i = 0; i < DataManagers.Instance.dicChestDatas.Count; i++)
        {
            GameObject cellGO = Instantiate(chestPrefab, contents.transform);
            cells[i] = cellGO.GetComponent<ChestCellView>();

        }



        int idx = 100;

        for (int i = 0; i < cells.Length; i++)
        {

            cells[i].chestName.text = DataManagers.Instance.dicChestDatas[idx].ChestName;
            cells[i].chestImage.sprite = atlas.GetSprite(DataManagers.Instance.dicChestDatas[idx].ImageName);
            cells[i].price.text = Convert.ToString(DataManagers.Instance.dicChestDatas[idx].Price);

            idx++;

        }
    }

   
}

6. UIChestScrollView.cs (content의 cell 동적 생성과 생성된 cell에 데이터 뿌려주기)

DataManager의 딕셔너리의 Count 만큼 Cell 동적 생성

 

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ChestCellView : MonoBehaviour 
{
    public TMP_Text chestName;
    public Image chestImage;
    public TMP_Text price;



}

7. ChestCellView.cs

 

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ChestData
{
    public int id;
    public string ChestName;
    public string ImageName;
    public int Price;

}

8. ChestData.cs

 

 

 

------------------------------------------------------------------------------------------------------------------------------------------------------------

 

9. KeyNotFoundException 오류

 

데이터를 로드(역직렬화)해서 데이터를 내가 원하는 곳에 뿌려줄때 

 

데이터를 로드하는 순서를 제일 먼저 보장해주자 (Awake > Start)

 

위의 오류는 순서를 둘 다 Start에 넣었을 때 나는 오류이다.

 

처음에는 잘 되다가 유니티를 종료했다가 다시 실행하니 갑자기 저런 오류가 생겨서 

 

로드의 순서를 Awake로 바꿔주니 해결 되었다. 

 

 

+ Recent posts