유니티 심화

LearnUGUI - Test05 [Shop] - 데이터 저장/ 불러오기 , json, DataManager, ChestData, Prefab

다모아 2023. 9. 7. 18:13

 

Test05UIScrollView

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

public class Test05UIChestScrollView : MonoBehaviour
{
    [SerializeField]
    private Transform contentTr;
    [SerializeField]
    private GameObject uiChestCellPrefab;
    [SerializeField]
    private GameObject uiChestCellAdPrefab;

    private List<Test05UIChestCell> cellList = new List<Test05UIChestCell>();
    
    public void Init()
    {
        List<ChestData> chestDatas = DataManager.instance.GetChestDatas();
        foreach(ChestData data in chestDatas)
        {
            Debug.LogFormat("<color=yellow>[UIChestScrollView] {0}, {1}, {2}, {3}, {4}</color>", data.id, data.name, data.type, data.price, data.sprite_name);
            
            Test05UIChestCell cell = null;

            if((Test05UIChestCell.eChestType)data.type == Test05UIChestCell.eChestType.Wooden) 
            {
                //Wooden이라면 Wooden 프리팹 생성
                GameObject go = Instantiate(this.uiChestCellAdPrefab, this.contentTr);
                cell = go.GetComponent<Test05UIChestCellAd>();
                Test05UIChestCellAd cellAd = cell as Test05UIChestCellAd;
                cellAd.onClickAd = () => {
                    Debug.LogFormat("<color=yellow>{0}, 광고보기</color>", cellAd.ChestType);
                };
                cellAd.onClickPrice = () => {
                    Debug.LogFormat("<color=yellow>{0}, {1}</color>", cell.ChestType, cell.Price);
                };
            }
            else
            {
                //Wooden이 아닌경우 프리팹 생성?
                GameObject go = Instantiate(this.uiChestCellPrefab, this.contentTr);
                cell = go.GetComponent<Test05UIChestCell>();
                cell.onClickPrice = () => {
                    Debug.LogFormat("<color=yellow>{0}, {1}</color>", cell.ChestType, cell.Price);
                };
            }
            cell.Init();

            this.cellList.Add(cell);
        }
    }
}

 

DataManager

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataManager
{
    public static readonly DataManager instance = new DataManager();

    public Dictionary<int, ChestData> dicChest = new Dictionary<int, ChestData>();

    public void LoadChestData()
    {
        TextAsset asset = Resources.Load<TextAsset>("Chest_data");
        Debug.Log(asset);

        ChestData[] datas = JsonConvert.DeserializeObject<ChestData[]>(asset.text);

        foreach(ChestData data in datas)
        {
            this.dicChest.Add(data.id, data);
        }
    }

    public List<ChestData> GetChestDatas()
    {
        List<ChestData> list = new List<ChestData>();
        foreach (ChestData data in this.dicChest.Values)
        {
            list.Add(data);
        }
        return list;
    }
}

 

Test05Main

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

public class Test05Main : MonoBehaviour
{
    [SerializeField]
    private Test05UIChestScrollView uiChestScrollView;

    private void Start()
    {
        DataManager.instance.LoadChestData();

        this.uiChestScrollView.Init();
    }
}

 

ChestData

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;
}