유니티 심화

LearnUGUI - Test05 [Shop]

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


Test05ChestCell

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

public class Test05UIChestCell : MonoBehaviour
{
    //상자타입
    public enum eChestType
    {
        Wooden, Silver, Golden, Epic, Legendary
    }

    //구매버튼
    [SerializeField]
    protected Button btnBuy;
    //상자타입
    [SerializeField]
    protected eChestType chestType;

    public eChestType ChestType
    {
        get
        {
            return this.chestType;
        }
    }

    //가격
    [SerializeField]
    protected int price;

    public int Price => this.price;

    public Action onClickPrice;
    public virtual void Init()
    {
        Debug.LogFormat("[Test05UIChestCell] Init : {0}", this.chestType);

        this.btnBuy.onClick.AddListener(() => {
            Debug.LogFormat("{0}, {1}", this.chestType, this.price);
            this.onClickPrice();
        });
    }
}

 

Test05ChestCellAd

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

public class Test05UIChestCellAd : Test05UIChestCell
{
    //광고버튼
    [SerializeField]
    private Button btnAd;

    public Action onClickAd;
    public override void Init()
    {
        base.Init();
        
        Debug.LogFormat("[UIChestCellAd] Init : {0}", this.chestType);

        this.btnAd.onClick.AddListener(() => {
            Debug.LogFormat("{0}, 광고보기", this.chestType);
            this.onClickAd();
        });
    }
}

 

Test05ChestScrollView

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

public class Test05UIChestScrollView : MonoBehaviour
{
    [SerializeField]
    private Test05UIChestCell[] uiChestCells;

    private void Start()
    {
        for(int i = 0; i < uiChestCells.Length; i++)
        {
            Test05UIChestCell uiChestCell = this.uiChestCells[i];

            uiChestCell.onClickPrice = () => {
                //가격을 가져오기
                Debug.LogFormat("<color=yellow>{0}, {1}</color>", uiChestCell.ChestType, uiChestCell.Price);
            };

            Test05UIChestCell.eChestType chestType = (Test05UIChestCell.eChestType)i;
            if(chestType == Test05UIChestCell.eChestType.Wooden)
            {
                Test05UIChestCellAd uiChestCellAd = (Test05UIChestCellAd)uiChestCell; //자식 상속 클래스라 가능하다.
                uiChestCellAd.onClickAd = () => {
                    Debug.LogFormat("<color=yellow>{0}, 광고보기</color>", uiChestCellAd.ChestType);
                };
            }

            uiChestCell.Init();
        }
    }
}

 

도움 - base.Init();

https://daily50.tistory.com/520

 

Base에 대하여 [Unity]

Base 란, 기본적으로 해당 키워드를 사용하는 클래스의 부모 클래스를 가리키는 것이다. 아래와 같은 코드가 있다고 가정해보자 . class Fruit { string g_name; int g_grade; int g_price; public Fruit(string name, int

daily50.tistory.com