유니티 심화/복습

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

다모아 2023. 9. 10. 22:14

Shop_Gold를 할 때는 GoldIcon.transform.position을 저 pos_x, pos_y로 할 때 자꾸 그림이 아래에 리스폰 되서 음.. 왜이러지 하고 그냥 수작업으로 다 위치 조정을 해줬었는데 ..

[ 그래서 나온 위치가 x는 41에다가 내가 알아온 x위치 , y는 800에다가 내가 알아온 y위치였다.] 

근데 너무 멍청하게해서 2번째 Gem할 때는 다른 방식으로 해봐야지.. 하고 Gem을 하면서 생각해보니

localPosition을 쓰면 내가 구한 위치로 오는거였다;;


gem_data.json
0.00MB
gem_data.xlsx
0.01MB

 

HW02AtlasManager

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

public class HW02AtlasManager : MonoBehaviour
{
    public static HW02AtlasManager instance;

    [SerializeField]
    private string[] arrAtlasNames;

    private Dictionary<string, SpriteAtlas> dicAtlas = new Dictionary<string, SpriteAtlas>();

    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.dicAtlas.Add(atlasName, atlas);
        }
        Debug.LogFormat("{0}개의 Atlas로드", this.dicAtlas.Count);
    }

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

 

HW02DataManager

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

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

    private Dictionary<int, GemData> dicGemDatas = new Dictionary<int, GemData>();
    public void LoadGemDatas()
    {
        TextAsset asset = Resources.Load<TextAsset>("gem_data");
        string json = asset.text;
        GemData[] gemDatas = JsonConvert.DeserializeObject<GemData[]>(json);
        this.dicGemDatas = gemDatas.ToDictionary(x => x.id);
        Debug.LogFormat("{0}개 Data로드", this.dicGemDatas.Count);
    }

    public List<GemData> GetGemDatas()
    {
        return this.dicGemDatas.Values.ToList();
    }
}

 

GemData

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

public class GemData
{
    public int id;
    public string name;
    public float cash_price;
    public int game_price;
    public string sprite_name;
    public int width;
    public int height;
    public int pos_x;
    public int pos_y;
}

 

HW02UIGemCell

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

public class HW02UIGemCell : MonoBehaviour
{
    [SerializeField]
    private Button btnCashPrice;
    [SerializeField]
    private TMP_Text txtGamePrice;
    [SerializeField]
    private TMP_Text txtCashPrice;
    [SerializeField]
    private TMP_Text txtGameName;
    [SerializeField]
    private Image gemIcon;

    public Action onClick;
    public void Init(GemData data)
    {
        this.LoadDatas(data);

        this.btnCashPrice.onClick.AddListener(() => {
            Debug.LogFormat("{0}, {1}", data.id, data.name);
            this.onClick();
        });
    }
    private void LoadDatas(GemData data)
    {
        string cash_price = string.Format("US ${0}", data.cash_price);
        this.txtCashPrice.text = cash_price;
        this.txtGameName.text = data.name;
        this.txtGamePrice.text = data.game_price.ToString();
        SpriteAtlas atlas = HW02AtlasManager.instance.GetAtlas("HW02UIAtlas");
        this.gemIcon.sprite = atlas.GetSprite(data.sprite_name);
        this.gemIcon.SetNativeSize();
        this.gemIcon.rectTransform.sizeDelta = new Vector2(data.width, data.height);
        this.gemIcon.transform.localPosition = new Vector2(data.pos_x, data.pos_y);
    }
}

 

HW02UIGemScrollView

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

public class HW02UIGemScrollView : MonoBehaviour
{
    [SerializeField]
    private GameObject cellPrefab;
    [SerializeField]
    private Transform contentTr;

    private List<HW02UIGemCell> cellList = new List<HW02UIGemCell>();
    public void Init()
    {
        List<GemData> datas = HW02DataManager.instance.GetGemDatas();

        foreach(GemData data in datas)
        {
            HW02UIGemCell cell = null;
            GameObject go = Instantiate(this.cellPrefab, this.contentTr);
            cell = go.GetComponent<HW02UIGemCell>();
            cell.Init(data);
            this.cellList.Add(cell);
            cell.onClick = () => {
                Debug.LogFormat("<color=yellow>{0}, {1}</color>", data.id, data.name);
            };
        }
    }
}

 

HW02UIMain

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

public class HW02UIMain : MonoBehaviour
{
    [SerializeField]
    private HW02UIGemScrollView uiScrollView;

    // Start is called before the first frame update
    void Start()
    {
        HW02DataManager.instance.LoadGemDatas();
        HW02AtlasManager.instance.LoadAtlasDatas();

        this.uiScrollView.Init();
    }
}