유니티 심화/복습

[복습] LearnUGUI - Practice05 [Shop] 잘 몰라서 메모장에 적어놓고 연습

다모아 2023. 9. 7. 21:48

DataManager 안 쓰는거 먼저

 

새로운 Practice05 Scene을 만들어서 연습

 

1. guide 만들어주기

guide

 

2. main 빈 오브젝트 만들어주기, UIScrollView 만들어주기

UIScrollView

3. scroll Rect, mask, horizontal layout group, Content Size cutter 추가

 

 

UIScrollView
content

4. 이미지 ChestCell들 이미지 만들어주기

 

5. 메모장에 적은거 보고 코딩해보기 [DataManager X]

Practice05UIChestCell

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

public class Practice05UIChestCell : MonoBehaviour
{
    public enum eChestType
    {
        Wooden, Silver, Golden, Epic, Legendary
    }

    [SerializeField]
    private eChestType chestType;

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

    [SerializeField]
    private int price;

    protected int Price => this.price;

    [SerializeField]
    protected Button btnPrice;

    public virtual void Init()
    {
        this.btnPrice.onClick.AddListener(() => {
            Debug.LogFormat("{0}, {1}", this.chestType, this.Price);
        });
    }
}

 

Practice05UIChestCellAd

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

public class Practice05UIChestCellAd : Practice05UIChestCell
{
    [SerializeField]
    private Button btnAd;

    public override void Init()
    {
        //부모꺼 사용
        base.Init();

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

 

Practice05UIChestScrollView

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

public class Practice05UIChestScrollView : MonoBehaviour
{
    [SerializeField]
    private Practice05UIChestCell[] uiChestCells;

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

 

메모장에 적고나서 해보니 더 쉽게 다가오는거 같다.