유니티 심화

[UI] 스크롤뷰

송현호 2023. 9. 7. 14:28
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

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

    [SerializeField]
    protected Button buyButton;
    [SerializeField]
    private Transform content;

    public System.Action onClickPrice;

    public eChestType type;
    public int price;

    // Start is called before the first frame update
    void Start()
    {
        

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public virtual void Init()
    {
        buyButton.onClick.AddListener(() =>
        {
            onClickPrice();
        });
    }
}
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;

public class UIChestCellAd : UIChestCell
{
    [SerializeField]
    private Button adButton;

    public System.Action onClickAd;
    // Start is called before the first frame update
    void Start()
    {
        

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public override void Init()
    {
        base.Init();

        adButton.onClick.AddListener(() =>
        {
            onClickAd();
        });
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.WSA;
using static UIChestCell;

public class UIScrollView : MonoBehaviour
{
    [SerializeField]
    private UIChestCell[] uiChestCell;
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < uiChestCell.Length; ++i)
        {
            uiChestCell[i].Init();
            UIChestCell cell = uiChestCell[i];
            cell.type = (UIChestCell.eChestType)i;
            Debug.Log(cell.type);
            if (cell.type == (UIChestCell.eChestType)0)
            {
                cell.price = 100;
            }
            else if (cell.type == (UIChestCell.eChestType)1)
            {
                cell.price = 600;
            }
            else if (cell.type == (UIChestCell.eChestType)2)
            {
                cell.price = 1000;
            }
            else if (cell.type == (UIChestCell.eChestType)3)
            {
                cell.price = 3000;
            }
            else if (cell.type == (UIChestCell.eChestType)4)
            {
                cell.price = 6000;
            }
            cell.onClickPrice = () =>
            {
                Debug.LogFormat("{0},{1}", cell.type, cell.price);
            };

            if (cell == uiChestCell[0])
            {
                UIChestCellAd uiChestCellAd = (UIChestCellAd)uiChestCell[0];
                uiChestCellAd.onClickAd = () =>
                {
                    Debug.Log("광고 시청");
                };
            }


            


        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    
}

코드를 짜고 나서 다른 사람 코드를 봤는데 type이랑 price에 값을 할당하지 않아서 당황했다 그래서 새씬을 파서 실험해보니까 인스펙터에서 할당한거였다...  

 

+ 데이터를 읽어와서 동적할당 했을때

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

public class UIChestCellAd : UIChestCell
{
    [SerializeField]
    private Button adButton;

    public System.Action onClickAd;
    // Start is called before the first frame update
    void Start()
    {
        

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public override void Init(ChestData dicChestDatas, Sprite sprite)
    {
        base.Init(dicChestDatas, sprite);

        adButton.onClick.AddListener(() =>
        {
            onClickAd();
        });
    }
}

 

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

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

    [SerializeField]
    protected Button buyButton;
    [SerializeField]
    protected TMPro.TMP_Text buttonText;

    [SerializeField]
    protected Image chestImage;

    private ChestData data;

    public System.Action onClickButton;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public virtual void Init(ChestData dicChestDatas, Sprite sprite)
    {
        this.data = dicChestDatas;
        this.chestImage.sprite = sprite;
        this.buttonText.text = dicChestDatas.price.ToString();

        this.buyButton.onClick.AddListener(() =>
        {
            onClickButton();
        });
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.WSA;
using UnityEngine.U2D;

public class UIScrollView : MonoBehaviour
{
    [SerializeField]
    private GameObject uiChestCellAd;
    [SerializeField]
    private GameObject uiChestCell;
    [SerializeField]
    private Transform content;
    [SerializeField]
    private SpriteAtlas atlas;
    // Start is called before the first frame update
    void Start()
    {
        DataManager.instance.Init();
        var chestDatas = DataManager.instance.GetDicChestDatas();
        for(int i = 0; i<chestDatas.Count; i++)
        {
            var data = chestDatas[100+i];
            Sprite sprite = this.atlas.GetSprite(data.sprite_name);
            if (data.type == 0) {
                GameObject go = Instantiate(uiChestCellAd,content);
                var cell = go.GetComponent<UIChestCellAd>();
                cell.Init(data, sprite);
                cell.onClickAd = () =>
                {
                    Debug.Log("광고 시청");
                };
                cell.onClickButton = () =>
                {
                    Debug.LogFormat("{0}, {1}", data.name, data.price);
                };
            }
            else
            {
                GameObject go = Instantiate(uiChestCell, content);
                var cell = go.GetComponent<UIChestCell>();
                cell.Init(data, sprite);
                cell.onClickButton = () =>
                {
                    Debug.LogFormat("{0}, {1}", data.name, data.price);
                };

            }
        }

        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    
}

for문을 도는데 에러가 나는 이유를 몰랐는데 알고보니 uiChestCellAd스크립트에 제대로 객체를 할당하지 않아서 였다. 체감상 코드짜는시간보다 이거 찾는데 시간이 더 많이 걸린 것 같다...