유니티 심화/복습

[복습] LearnUGUI - Stage

다모아 2023. 9. 6. 01:00

수업 중간에 빠져서 뭐를 요구하는지 몰라서 코드를 보고 따라쳤다..

 

Test03UIMain

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

public class Test03UIMain : MonoBehaviour
{
    [SerializeField]
    private Test03UIStage uiStage;

    [SerializeField]
    private Button[] arrBtns;

    // Start is called before the first frame update
    void Start()
    {
        this.uiStage.Init(1);
        for (int i = 0; i < this.arrBtns.Length; i++)
        {
            Button btn = this.arrBtns[i];
            int idx = i;
            btn.onClick.AddListener(() => {
                var state = (Test03UIStage.eState)idx;
                Debug.LogFormat("{0}, {1}", idx, state);
                this.uiStage.ChangeState(state);
            });
        }
    }

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

 

Test03UIStage

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

public class Test03UIStage : MonoBehaviour
{
    public enum eState
    {
        Lock, Doing, Complete
    }

    [SerializeField]
    private TMP_Text[] arrTxtStageNum;

    [SerializeField]
    private GameObject[] arrStateGo;

    private eState state;

    public void Init(int stageNum)
    {
        foreach (var tmpText in this.arrTxtStageNum)
        {
            tmpText.text = stageNum.ToString();
        }
        this.ChangeState(eState.Lock);
    }
    public void ChangeState(eState state)
    {
        this.InActiveAll();

        this.state = state;
        int index = (int)this.state;
        this.arrStateGo[index].SetActive(true);
    }

    private void InActiveAll()
    {
        foreach(var go in this.arrStateGo)
        {
            go.SetActive(false);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

'유니티 심화 > 복습' 카테고리의 다른 글

[복습] LearnUGUI - Test05[Shop]  (0) 2023.09.07
[복습] LearnUGUI - Test04[Stage]  (2) 2023.09.07
[복습]LearnUGUI - Button, Toggle  (0) 2023.09.05
[복습] 동기/ 비동기  (0) 2023.08.31
프리팹 parent 변경 실수  (0) 2023.08.30