728x90
SceneMain
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test01Main : MonoBehaviour
{
[SerializeField]
private Test01UIMain uiMain;
void Start()
{
//uiMain에 입력된 버튼 타입을 가져와서 출력
this.uiMain.onButtonClicked = (btnType) =>
{
Debug.Log(btnType);
};
this.uiMain.Init();
}
}
Test01UIMain
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class Test01UIMain : MonoBehaviour
{
public enum eButtonType
{
Blue, Red, Green, Purple
}
[SerializeField]
private Button[] btns;
[SerializeField]
private Test01UITabController uiTabController;
[SerializeField]
private Test01UISlider uiSlider;
public System.Action<eButtonType> onButtonClicked;
public void Init()
{
this.uiTabController.Init();
this.uiSlider.onValueChanged = (val) =>
{
Debug.LogFormat("[Test01UIMain] onValueChanged: {0}", val);
};
//min. max 값 8,16으로 변경
this.uiSlider.Init(8, 16);
for(int i = 0; i < btns.Length; i++)
{
Button btn = this.btns[i];
Debug.Log(btn.name);
int index = i; //캡쳐
btn.onClick.AddListener(() =>
{
//클로져 : 람다 안에서 상위 스코프 변수에 접근
eButtonType selectedButtonType = (eButtonType)index;
Debug.Log(selectedButtonType);
//이벤트 전송
this.onButtonClicked(selectedButtonType);
});
}
//this.btnBlue.onClick.AddListener(() => {
// Debug.Log("blue 버튼 클릭 됨!");
// this.onButtonClicked(eButtonType.Blue);
//});
//this.btnRed.onClick.AddListener(() =>
//{
// Debug.Log("red 버튼 클릭 됨!");
// this.onButtonClicked(eButtonType.Red);
//});
//this.btnGreen.onClick.AddListener(() =>
//{
// Debug.Log("green 버튼 클릭 됨!");
// this.onButtonClicked(eButtonType.Green);
//});
//this.btnPurple.onClick.AddListener(() =>
//{
// Debug.Log("purple 버튼 클릭 됨!");
// this.onButtonClicked(eButtonType.Purple);
//});
}
}
Toggle
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test01UIToggle : MonoBehaviour
{
public enum eState
{
On,
Off
}
//맴버변수
private eState state;
[SerializeField]
private Button btn;
[SerializeField]
private GameObject[] arrOnOff;
private void Start()
{
//var go = this.arrOnOff[(int)this.state];
//go.SetActive(true);
this.btn.onClick.AddListener(() =>
{
var preState = this.state;
//이전 상태 게임오브젝트 비활성화
this.arrOnOff[(int)this.state].SetActive(false);
Debug.LogFormat("토글 버튼 클릭 : {0}", this.state);
//현재 상태를 발견
if(this.state == eState.On)
{
this.state = eState.Off;
}
else
{
this.state = eState.On;
}
Debug.LogFormat("{0} -> {1}", preState, this.state);
//현재 상태 게임오브젝트 비활성화
this.arrOnOff[(int)this.state].SetActive(true);
});
}
}
TabController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test01UITabController : MonoBehaviour
{
public enum eMenuType
{
Package, Card, Equipment
}
[SerializeField]
private Test01UITab_[] uiTabs;
[SerializeField]
private string[] arrMenuNames;
private eMenuType selectedMenuType;
public void Init()
{
for(int i = 0; i < this.uiTabs.Length; i++)
{
int idx = i;
var uiTab = this.uiTabs[i];
uiTab.Init(this.arrMenuNames[i]);
uiTab.btn.onClick.AddListener(() =>
{
Debug.Log(idx);
this.SelectMenu((eMenuType)idx);
});
}
this.SelectMenu(eMenuType.Package);
}
private void SelectMenu(eMenuType menuType)
{
foreach(var tab in this.uiTabs)
{
tab.UnSelect();
}
int idx = (int)menuType;
var uiTab = this.uiTabs[idx];
uiTab.Select();
this.selectedMenuType = menuType;
Debug.Log(this.selectedMenuType);
}
}
Tab_1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test01UITab : MonoBehaviour
{
public enum eState
{
Focus, Default
}
public eState state;
[SerializeField]
private Button btn;
[SerializeField]
private GameObject[] arrFocDef;
void Start()
{
this.btn.onClick.AddListener(() => {
var preState = this.state;
//이전 상태 게임오브젝트 비활성화
this.arrFocDef[(int)this.state].SetActive(false);
//현재 상태를 반전
if (this.state == eState.Focus)
{
this.state = eState.Default;
}
else
{
this.state = eState.Focus;
}
Debug.LogFormat("{0} -> {1}", preState, this.state);
//현재 상태의 게임오브젝트 활성화
this.arrFocDef[(int)this.state].SetActive(true);
});
}
}
Tab_2
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Test01UITab_ : MonoBehaviour
{
[SerializeField]
private TMP_Text[] arrText;
[SerializeField]
private GameObject selectedGo;
[SerializeField]
private GameObject unSelectedGo;
public Button btn;
public void Init(string menuName)
{
Debug.Log(menuName);
foreach(var tmpText in this.arrText)
{
tmpText.text = menuName;
}
}
public void Select()
{
this.selectedGo.SetActive(true);
this.unSelectedGo.SetActive(false);
}
public void UnSelect()
{
this.selectedGo.SetActive(false);
this.unSelectedGo.SetActive(true);
}
}
Slider
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Test01UISlider : MonoBehaviour
{
[SerializeField]
private Slider slider;
[SerializeField]
private TMP_Text txtMin;
[SerializeField]
private TMP_Text txtMax;
public System.Action<float> onValueChanged;
public void Init(float min, float max)
{
this.slider.minValue = min;
this.slider.maxValue = max;
//매개변수 값을 문자형식으로 변환
this.txtMin.text = this.slider.minValue.ToString();
this.txtMax.text = this.slider.maxValue.ToString();
this.slider.onValueChanged.AddListener((val) =>
{
this.onValueChanged(val);
});
}
}
'KDT > 유니티 심화' 카테고리의 다른 글
23/09/05 LearnUGUI (UIStage) (0) | 2023.09.05 |
---|---|
23/09/05 LearnUGUI (InputField) (0) | 2023.09.05 |
23/09/04 LearnUGUI 버튼2 (배열로 관리) (0) | 2023.09.04 |
23/09/04 LearnUGUI 버튼 (0) | 2023.09.04 |
23/09/03 [주말] HeroShooter 총알 발사 (1) | 2023.09.03 |