Unity/유니티 기본

[4.27] Daily Login Reward 화면 - NGUI

ljw4104 2021. 4. 27. 17:06

결과물

Daily.cs

날마다 아이템박스 출력, 이번에 받는 아이템은 for문으로 계속 순회하다 처음으로 user의 check가 false가 될 때로 선택함.

using UnityEngine;

public class Daily : MonoBehaviour
{
    public GameObject prefab;
    public UIGrid grid;
    public UILabel completeDay;
    public UILabel allDay;

    private bool isFirst = true;
    private bool isFirstFalse;              //Now 판별을 위해
    private int complete;
    private int all;
    // Start is called before the first frame update
    void Start()
    {
        DataManager.Instance.GetDailyData();
        this.complete = 0;
        this.Init();
    }

    private void Init()
    {
        for(int i = 0; i < DataManager.Instance.arrDaily.Count; i++)
        {
            var daily = DataManager.Instance.arrDaily[i + 1];
            var user = DataManager.Instance.arrDailyInfo[i + 1];
            if(!user.check && this.isFirst)
            {
                this.isFirstFalse = true;
                this.isFirst = false;
            }

            var go = Instantiate(this.prefab, this.grid.transform);
            go.GetComponent<DailyReward>().Init(user.check, daily.id, daily.sprite_name, daily.amount, this.isFirstFalse);

            if (this.isFirstFalse)
            {
                this.isFirstFalse = false;
            }

            if (user.check)
            {
                this.complete++;
            }
            this.all++;
        }
        grid.Reposition();

        this.completeDay.text = this.complete.ToString();
        this.allDay.text = this.all.ToString();
    }
}

 

DailyReward.cs

이미 지나온 날, 아직 오지않은 날을 구분해서 프리팹을 생성함.

using UnityEngine;

public class DailyReward : MonoBehaviour
{
    public GameObject doing;
    public GameObject complete;
    public UILabel date;
    public UILabel itemNum;
    public UISprite item;
    public UISprite now;

    public void Init(bool isComplete, int date, string path, int num, bool isNow)
    {
        this.now.gameObject.SetActive(false);
        this.date.text = date.ToString();
        this.itemNum.text = num.ToString();
        this.item.spriteName = path;
        this.item.MakePixelPerfect();

        if (!isComplete)
        {
            this.complete.SetActive(false);
            if (isNow)
            {
                this.now.gameObject.SetActive(true);
            }
            this.item.depth++;
        }
        else
        {
            this.doing.SetActive(false);
        }
    }
}

Prefab 구조 / Doing과 Complete가 둘 다 켜져있고, 프리팹이 생성될 때 둘 중 하나가 꺼진다.
DailyRewardData.xlsx
0.01MB
DailyRewardInfo.xlsx
0.01MB