유니티 심화

LearnUGUI

다모아 2023. 9. 4. 16:52

 

Test01UITab

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

public class Test01UITab : MonoBehaviour
{
    public Button btn;

    [SerializeField]
    private GameObject selectGo;
    [SerializeField]
    private GameObject unselectGo;
    //정보를 알고있어야함

    public void Selected()
    {
        this.selectGo.SetActive(true);
        this.unselectGo.SetActive(false);
    }

    public void UnSelected()
    {
        this.unselectGo.SetActive(true);
        this.selectGo.SetActive(false);
    }
}

 

Test01UITabController

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

public class Test01UITabController : MonoBehaviour
{
    private enum eMenuType
    {
        Tab0, Tab1, Tab2
    }

    [SerializeField]
    private Test01UITab[] uiTabs;

    private eMenuType eSelectedMenuType;
    //기능구현
    public void Init()
    {
        for(int i = 0; i < this.uiTabs.Length; i++)
        {
            Test01UITab uitab = this.uiTabs[i];
            int index = i;
            uitab.btn.onClick.AddListener(() => {
                this.SelectMenu((eMenuType)index);
            });
        }
    }

    private void SelectMenu(eMenuType menuType)
    {
        foreach(Test01UITab tab in uiTabs)
        {
            tab.UnSelected();
        }

        int idx = (int)menuType;
        Test01UITab uitab = this.uiTabs[idx];
        uitab.Selected();
        this.eSelectedMenuType = menuType;
    }
}

 

Test01UIMain

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

public class Test01UIMain : MonoBehaviour
{
    public enum eButtonType
    {
        Blue, Red, Green, Purple
    }

    [SerializeField]
    private Button[] btns;

    public Action<eButtonType> onButtonClicked;

    [SerializeField]
    private Test01UITabController uiTabController;

    public void Init()
    {
        this.uiTabController.Init();

        for (int i = 0; i < this.btns.Length; i++)
        {
            Button btn = this.btns[i];
            int index = i; //캡쳐
            Debug.Log(btn.name);
            btn.onClick.AddListener(() => {
                //클로져 : 람다 안에서 상위 스코프 변수에 접근
                Debug.Log((eButtonType)index);
                eButtonType selectedButtonType = (eButtonType)index;
                this.onButtonClicked(selectedButtonType);
            });

        }
    }
}

 

Test01UICheckBox

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

public class Test01UICheckBox : MonoBehaviour
{
    private enum eState
    {
        On, Off
    }

    private eState state;

    [SerializeField]
    private Button btn;

    [SerializeField]
    private GameObject[] arrOnOff;

    // Start is called before the first frame update
    void Start()
    {
        btn.onClick.AddListener(() => {
            this.arrOnOff[(int)state].SetActive(false);
            if (this.state == eState.On)
            {
                this.state = eState.Off;
            }
            else
            {
                this.state = eState.On;
            }
            this.arrOnOff[(int)state].SetActive(true);
        });
    }

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

 

Test01UIToggle

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

public class Test01UIToggle : MonoBehaviour
{

    public enum eState
    {
        On, Off
    }

    //멤버변수
    private eState state; // 기본값 : 0
    [SerializeField]
    private Button btn;

    [SerializeField]
    private GameObject[] arrOnOff;
    // Start is called before the first frame update
    void Start()
    {
        this.btn.onClick.AddListener(() => {
            Debug.LogFormat("토글 버튼 클릭 : {0}", this.state);
            //이전 상태 게임오브젝트 비활성화
            this.arrOnOff[(int)this.state].SetActive(false);

            //현재 상태를 반전
            if (this.state == eState.On)
            {
                this.state = eState.Off;
            }
            else
            {
                this.state = eState.On;
            }
            //현재 상태 게임오브젝트 활성화
            this.arrOnOff[(int)this.state].SetActive(true);

        });    
    }
}

 

Test01Main

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

public class Test01Main : MonoBehaviour
{
    [SerializeField]
    private Test01UIMain uiMain;

    // Start is called before the first frame update
    void Start()
    {
        this.uiMain.onButtonClicked = (btnType) => {
            Debug.Log(btnType);
        };
        this.uiMain.Init();
    }
}

TextMin, Max위치가 잘못되서 수정해줌

 

 

Test01UISlider

using System;
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 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);
        });
    }
    // Start is called before the first frame update
    void Start()
    {
        
        
    }
}

 

Test01UIMain

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

public class Test01UIMain : MonoBehaviour
{
    public enum eButtonType
    {
        Blue, Red, Green, Purple
    }

    [SerializeField]
    private Button[] btns;

    public Action<eButtonType> onButtonClicked;

    [SerializeField]
    private Test01UITabController uiTabController;

    [SerializeField]
    private Test01UISlider uiSlider;
    public void Init()
    {
        this.uiTabController.Init();

        this.uiSlider.onValueChanged = (val) => {
            Debug.Log(val);
        };
        this.uiSlider.Init(5, 10);

        for (int i = 0; i < this.btns.Length; i++)
        {
            Button btn = this.btns[i];
            int index = i; //캡쳐
            Debug.Log(btn.name);
            btn.onClick.AddListener(() => {
                //클로져 : 람다 안에서 상위 스코프 변수에 접근
                Debug.Log((eButtonType)index);
                eButtonType selectedButtonType = (eButtonType)index;
                this.onButtonClicked(selectedButtonType);
            });

        }
    }
}

 

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

fbx 파일 애니메이션  (0) 2023.09.05
LearnUGUI - Test02 [UIPopupName]  (0) 2023.09.05
SaveAndLoad - UISlot  (0) 2023.09.01
SaveAndLoad - 캐릭터 프리팹 인스턴스화하기  (0) 2023.09.01
SaveAndLoad  (0) 2023.09.01