본문 바로가기
게임개발/게임 UI.UX 프로그래밍

인벤토리 그리드 만들기 3 : 아이템 팔기

by pudding81 2024. 2. 25.

 

 

 

 

 

 

팝업 만들기 - UI 캔버스 만들기

 

 

 

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

public class UIEnums
{ 
    public enum eItemType
    {
     Weaphon,
     shield,
     potion,
    Accessary

    }



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


public class UIPopupItemDetail : MonoBehaviour
{
    public TMP_Text txtItemType;
    public Image Itemicon;
    public TMP_Text txtItemName;
    public Button btnSell;
    public TMP_Text txtSellprice;
    public Button btndim;
    public int id;
    public System.Action<int> onSell;


   

    public void Init(int id)
    {
        this.id = id;
        var data =DataManager.Instance.GetItemData(id);
        var type = (UIEnums.eItemType)data.type;
        this.txtItemType.text = type.ToString();

        var atlas = AtlasManager.Instance.GetAtlas("UIInventoryAtlas");
        var sprite = atlas.GetSprite(data.sprite_name);
        this.Itemicon.sprite = sprite;

        this.txtItemName.text = data.name;
        this.txtSellprice.text = string.Format("{0}", data.sell_price);

        // 셀 버튼을 클릭하면
        this.btnSell.onClick.AddListener(OnSellActionHandler);
       
        this.txtSellprice.gameObject.SetActive(true);




    }

    public void OnSellActionHandler()
    {
       
            Debug.LogFormat("<color=magenta>sell item {0}</color>", this.id);
            this.onSell(this.id);

        
    }



    public void Open()
    {
        this.gameObject.SetActive(true);
    }

    public void Close()
    {
        this.gameObject.SetActive(false);
        this.btnSell.onClick.RemoveListener(OnSellActionHandler); //셀 버튼 액션 제거하기

    }

    

}

 

 

UIInventory 에서 아이템을 클릭하면 -> 팝업창 띄우기

 

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


public class UIInventory : MonoBehaviour
{
    public Scrollview scrollVeiw;
    public Button btnGetitem;
    public TMP_Text txtgold;
   
    public UIPopupItemDetail popupItemDetail;

    public void Init()
    {
        
        // 아이템 팔기
        this.popupItemDetail.onSell = (id) =>
        {
            this.SellItem(id);
        };

        this.GetItem();

        this.Popup();

        
    }


    public void SellItem(int id)
    {

        // 아이템 팔기
           
       
            Debug.LogFormat("팔려는 id {0}", id);

            var info =InfoManager.Instance.InventoryInfo.itemInfos.Find(x => x.id == id);

            Debug.LogFormat("찾은 아이템  id {0}", info.id);


            if (info.amount == 1)
            {              
                InfoManager.Instance.InventoryInfo.itemInfos.Remove(info);
            }
            else
            {       
                --info.amount;
            }

            
            //save
             InfoManager.Instance.SaveLocal();
             this.popupItemDetail.Close();
             this.scrollVeiw.Refresh();


            
            




    }


    public void Popup()
    {
        //아이템 디테일 팝업 띄우기
        this.scrollVeiw.onFocus = (id) =>
        {
            Debug.LogFormat("onfocus id {0}", id);
            this.popupItemDetail.Init(id);
            this.popupItemDetail.Open();
        };

        this.popupItemDetail.btndim.onClick.AddListener(() =>
        {
            this.popupItemDetail.Close();
        });


    }

    public void GetItem()
    {
        //Getitem btn을 클릭하면
        this.btnGetitem.onClick.AddListener(() =>
        {

            var data = DataManager.Instance.GetRandomItemData();
            var id = data.id;
            var foundInfo = InfoManager.Instance.InventoryInfo.itemInfos.Find(x => x.id == id);

            if (foundInfo == null)
            {
                ItemInfo info = new ItemInfo(id);
                InfoManager.Instance.InventoryInfo.itemInfos.Add(info); //최초아이템 획득

            }
            else
            {
                foundInfo.amount++;
            }

            //save
            InfoManager.Instance.SaveLocal();

            this.scrollVeiw.Refresh();



        });

        this.scrollVeiw.Init();


    }



}