유니티 심화/복습

[주말과제] LearnUGUI - HW01 [ Shop_Gold ]

다모아 2023. 9. 10. 20:47

gold_data.xlsx
0.01MB
gold_data.json
0.00MB

 

HW01AtlasManager

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

public class HW01AtlasManager : MonoBehaviour
{
    public static HW01AtlasManager instance;

    private Dictionary<string, SpriteAtlas> dicSpriteAtlas = new Dictionary<string, SpriteAtlas>();
    [SerializeField]
    private string[] arrAtlasNames;

    private void Awake()
    {
        if(instance != null && instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);
    }

    public void LoadAtlasDatas()
    {
        foreach(string atlasName in arrAtlasNames)
        {
            SpriteAtlas atlas = Resources.Load<SpriteAtlas>(atlasName);
            this.dicSpriteAtlas.Add(atlasName, atlas);
        }
        Debug.LogFormat("{0}개의 아틀라스 로드", this.dicSpriteAtlas.Count);
    }

    public SpriteAtlas GetAtlas(string atlasName)
    {
        return this.dicSpriteAtlas[atlasName];
    }
}

 

HW01DataManager

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

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

    private Dictionary<int, GoldData> dicGoldData = new Dictionary<int, GoldData>();
    public void LoadGoldDatas()
    {
        TextAsset asset = Resources.Load<TextAsset>("gold_data");
        string json = asset.text;
        //역직렬화 ,, 데이터 보관할 Dictionary
        GoldData[] goldDatas = JsonConvert.DeserializeObject<GoldData[]>(json);
        //데이터 Dictionary에 넣기
        this.dicGoldData = goldDatas.ToDictionary(x => x.id);
        Debug.LogFormat("{0}개의 Data로드", this.dicGoldData.Count);
    }
    
    public List<GoldData> GetGoldDatas()
    {
        return this.dicGoldData.Values.ToList();
    }
}

 

GoldData

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

public class GoldData
{
    //id	name	type	game_price	cash_price	sprite_name	width	height
    //int string int int float string int int

    public int id;
    public string name;
    public int type;
    public int game_price;
    public float cash_price;
    public string sprite_name;
    public int width;
    public int height;
    public int pos_x;
    public int pos_y;
}

 

HW01UIGoldCell

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

public class HW01UIGoldCell : MonoBehaviour
{
    public enum eCellType
    {
        Tiny, Fistful, Pouch, Box, Chest, Vault
    }

    private eCellType cellType;

    public eCellType CellType
    {
        get
        {
            return this.cellType;
        }
    }

    [SerializeField]
    private Button btnCashPrice;
    [SerializeField]
    private TMP_Text txtCashPrice;
    [SerializeField]
    private TMP_Text txtGamePrice;
    [SerializeField]
    private TMP_Text txtGoldName;
    [SerializeField]
    private Image uiGoldIcon;

    public Action onClick;
    public void Init(GoldData data)
    {
        this.LoadData(data);
    }

    private void LoadData(GoldData data)
    {
        this.txtCashPrice.text = "US $" + (data.cash_price.ToString());
        string game_price = string.Format("{0:#,###}", data.game_price);
        this.txtGamePrice.text = game_price + " Gold";
        this.txtGoldName.text = data.name;
        SpriteAtlas atlas = HW01AtlasManager.instance.GetAtlas("HW01UIAtlas");
        this.uiGoldIcon.sprite = atlas.GetSprite(data.sprite_name);
        this.uiGoldIcon.SetNativeSize();
        this.uiGoldIcon.rectTransform.sizeDelta = new Vector2(data.width, data.height);
        this.uiGoldIcon.transform.position = new Vector2(data.pos_x, data.pos_y);

        this.btnCashPrice.onClick.AddListener(() => {
            Debug.LogFormat("{0}, {1}", data.id, data.name);
            this.onClick();
        });
    }
}

 

HW01UIGoldScrollView

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class HW01UIGoldScrollView : MonoBehaviour
{
    [SerializeField]
    private GameObject cellPrefab;
    [SerializeField]
    private Transform contentTr;
    private List<HW01UIGoldCell> cellList = new List<HW01UIGoldCell>();
    public void Init()
    {
        List<GoldData> datas = HW01DataManager.instance.GetGoldDatas();
        foreach(GoldData data in datas)
        {
            HW01UIGoldCell cell = null;
            GameObject go = Instantiate(this.cellPrefab, this.contentTr);
            cell = go.GetComponent<HW01UIGoldCell>();
            cell.Init(data);
            this.cellList.Add(cell);
            cell.onClick = () => {
                Debug.LogFormat("<color=yellow>{0}, {1}</color>", data.id, data.name);
            };
        }
    }
}

 

HW01UIMain

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

public class HW01UIMain : MonoBehaviour
{
    [SerializeField]
    private HW01UIGoldScrollView uiScrollView;
    // Start is called before the first frame update
    void Start()
    {
        HW01DataManager.instance.LoadGoldDatas();
        HW01AtlasManager.instance.LoadAtlasDatas();

        this.uiScrollView.Init();
    }
}