1. 실행 이미지

 

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
{
    //버튼5개
    [SerializeField] private Button[] buttons;
    //텍스처 5개
    [SerializeField] SpriteAtlas spriteAtlas;
    private string prefix = "shop_img_chest_close_m_0";

    [SerializeField] private UIShopCellView uIShopCellView;

    enum ChestName
    {
        Wooden, Silver, Golden, Epic, Legendary
    }
    ChestName chestName;

    void Start()
    {
         for(int i=0; i<buttons.Length; i++)
        {
            string spriteName = string.Format("{0}{1}", prefix, i);


            this.spriteAtlas.GetSprite(spriteName);

            int idx = i;
            buttons[idx].onClick.AddListener(() =>
            {
                uIShopCellView.chestImage.sprite = this.spriteAtlas.GetSprite(spriteName);
                uIShopCellView.text.text = Enum.GetName(typeof(ChestName), idx) + " Chest";

            });


        }


    }

  
}

2. TestChestMain.cs

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

public class UIShopCellView : MonoBehaviour
{
    public Texture2D img0;
    public Image chestImage;
    public TMP_Text text;


    private void Start()
    {

        Sprite sprite = Sprite.Create(img0, new Rect(0, 0, img0.width, img0.height), new Vector2(0.5f, 0.5f));
        Debug.LogFormat("sprite: {0}", sprite);
        chestImage.sprite = sprite;
        chestImage.SetNativeSize();
        chestImage.GetComponent<RectTransform>().sizeDelta = new Vector2(307, 281);

    }
}

3. UIShopCellView.cs

+ Recent posts