유니티 심화

LearnUGUI - Test04[Stage]

다모아 2023. 9. 6. 18:02

스테이지를 넘어가면 리셋이 되긴하는데 1스테이지 정보가 안 남아있어서 1스테이지도 리셋된다.

 

이 부분만 잘 사용하면 정보를 남아있게 사용할 수 있을 것 같다.


Test04Stage

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

public class Test04Stage : MonoBehaviour
{
    public Button btnComplete;
    public Button btnDoing;
    public GameObject lockGo;
    public Action onClick;

    public TMP_Text txtComplete;
    public TMP_Text txtDoing;
    // Start is called before the first frame update
    void Start()
    {
        this.btnComplete.onClick.AddListener(() => {
            Debug.Log("스테이지 반복");
        });

        this.btnDoing.onClick.AddListener(() => {
            this.onClick();
            this.btnDoing.gameObject.SetActive(false);
            this.btnComplete.gameObject.SetActive(true);
        });
    }
}

 

Test04UIStageController

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

public class Test04UIStageController : MonoBehaviour
{

    [SerializeField]
    private Test04Stage[] stages;

    private int currentStageNum = 1;
    //초기화할 때 시작 스테이지 숫자 가져오기 / 1스테이지 : 1 , 2스테이지 : 19
    public void Init(int startStageNum, int currentStageNum)
    {
        for (int i = 0; i < this.stages.Length; i++)
        {
            int idx = i;
            if (this.currentStageNum != currentStageNum)
            {
                //초기화시키자
                this.Reset(i);
                Debug.LogFormat("{0} : 리셋", i);
            }
            //텍스트 변환
            this.ChangeText(i, startStageNum++);
            //대리자로 다음 스테이지 Lock 되어있는걸 Doing으로 바꿔주자
            this.stages[idx].onClick = () => {
                //이미 Doing 버튼을 누른 상태
                // stage + 1번의 스테이지를 Doing으로 바꿔주기
                if(idx == this.stages.Length - 1)
                {
                    Debug.Log("마지막");
                }
                else
                {
                    this.Activate(idx);
                    Debug.LogFormat("{0} : {1}", idx, this.stages.Length - 1);
                }
            };
        }
    }

    public int SendEndStageNum()
    {
        return this.stages.Length;
    }

    public void Activate(int idx)
    {
        this.stages[idx + 1].btnDoing.gameObject.SetActive(true);
        this.stages[idx + 1].lockGo.SetActive(false);
    }

    public void ChangeText(int idx, int startStageNum)
    {
        // idx 0을 받아오면 0에 1의 텍스트 부여
        this.stages[idx].txtComplete.text = startStageNum.ToString();
        this.stages[idx].txtDoing.text = startStageNum.ToString();
    }

    public void Reset(int idx)
    {
        this.stages[idx].btnComplete.gameObject.SetActive(false);
        this.stages[idx].btnDoing.gameObject.SetActive(false);
        this.stages[idx].lockGo.SetActive(true);
    }
}

 

Test04UIPageStage

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

public class Test04UIPageStage : MonoBehaviour
{
    [SerializeField]
    private Button btnLeft;
    [SerializeField]
    private Button btnRight;
    [SerializeField]
    private Test04UIStageController uiStageController;

    private int totalStage = 28; // 총 스테이지
    private int currentPageNum = 1; //현재 페이지
    private int endPageNum; //마지막 페이지
    private int endStageNum; // 마지막 스테이지
    private int startStageNum; //시작 스테이지
    void Start()
    {
        this.DisPlay();
        Debug.Log(this.startStageNum);
        this.uiStageController.Init(this.startStageNum, this.currentPageNum);
        this.btnLeft.onClick.AddListener(() => {
            if(this.currentPageNum == 1)
            {
                //1페이지라면 이동 불가능
                Debug.Log("이전페이지로 이동 불가능");
            }
            else
            {
                //이전 페이지로 이동
                this.currentPageNum -= 1;
                this.DisPlay();
                this.uiStageController.Init(this.startStageNum, this.currentPageNum);
            }
        });

        this.btnRight.onClick.AddListener(() => {
            if (this.currentPageNum == this.endPageNum)
            {
                //마지막 페이지라면 이동 불가능
                Debug.Log("다음페이지로 이동 불가능");
            }
            else
            {
                //다음 페이지로 이동
                this.currentPageNum += 1;
                this.DisPlay();
                this.uiStageController.Init(this.startStageNum, this.currentPageNum);
            }
        });
    }

    private void DisPlay()
    {
        this.endStageNum = this.uiStageController.SendEndStageNum(); // 마지막 스테이지 : 18
        this.endPageNum = Mathf.CeilToInt((float)this.totalStage / this.endStageNum);
        this.endStageNum = this.currentPageNum * this.endStageNum;
        Debug.LogFormat("총 스테이지 : {0}", this.totalStage);
        Debug.LogFormat("현재 페이지 : {0}", this.currentPageNum);
        Debug.LogFormat("최대 페이지 : {0}", this.endPageNum);
        this.startStageNum = this.endStageNum - 17;
        if(this.currentPageNum == this.endPageNum)
        {
            this.endStageNum -= 8;
        }
        Debug.LogFormat("스테이지 : {0} ~ {1}", this.startStageNum, this.endStageNum);
    }
}