728x90
버튼 누르면 해당 state에 해당하는 게임오브젝트 활성화시키기
Test02UIMain
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test03UIMain : MonoBehaviour
{
[SerializeField]
private Button[] arrBtns;
[SerializeField]
private UIStage uiStage;
[SerializeField]
private 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 = (UIStage.eState)idx;
Debug.LogFormat("{0},{1}", idx, state);
this.uiStage.ChangeState(state);
});
}
}
}
UIStage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class UIStage : MonoBehaviour
{
public enum eState
{
Lock, Doing, Complete
}
[SerializeField]
private TMP_Text[] arrTxtStageNum;
[SerializeField]
private GameObject[] arrStateGo; //0: lock, 1: doing, 2: complete
private eState state;
//private void Start()
//{
// //Test
// this.Init(1);
//}
public void Init(int stageNum)
{
foreach(var tmpText in this.arrTxtStageNum)
{
tmpText.text = stageNum.ToString();
}
this.ChangeState(eState.Lock);
}
//모든 스테이지 비활성화
private void InActiveAll()
{
foreach(var go in this.arrStateGo)
{
go.SetActive(false);
}
}
//스테이지 상태 변경
public void ChangeState(eState state)
{
this.InActiveAll();
this.state = state;
int index = (int)this.state;
this.arrStateGo[index].SetActive(true);
}
}
'KDT > 유니티 심화' 카테고리의 다른 글
23/09/06 LearnUGUI (Stage 2) (0) | 2023.09.06 |
---|---|
23/09/06 LearnUGUI (Stage 1) (0) | 2023.09.06 |
23/09/05 LearnUGUI (InputField) (0) | 2023.09.05 |
23/09/04 LearnUGUI 종합(Button,On/Off,Tab,Slider) (0) | 2023.09.04 |
23/09/04 LearnUGUI 버튼2 (배열로 관리) (0) | 2023.09.04 |