유니티 심화/복습

[복습]LearnUGUI - Button, Toggle

다모아 2023. 9. 5. 00:02

Button

PracticeUIButton

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

public class PracticeUIButton : MonoBehaviour
{
    public enum eColorType
    {
        Blue, Red
    }

    public Button[] btn;

    [SerializeField]
    private GameObject[] textColorGos;

    private eColorType eSelectedColorType;
    public void Init(eColorType colorType)
    {
        this.eSelectedColorType = colorType;
        Debug.Log(this.eSelectedColorType);
    }
}

 

PracticeUIMain

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

public class PracticeUIMain : MonoBehaviour
{
    [SerializeField]
    private PracticeUIButton uiButton;

    [SerializeField]
    private PracticeUIToggle uiToggle;
    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0; i < this.uiButton.btn.Length; i++)
        {
            int idx = i;
            this.uiButton.btn[i].onClick.AddListener(() => {
                this.uiButton.Init((PracticeUIButton.eColorType)idx);
            });
        }

        for(int i = 0; i < this.uiToggle.btn.Length; i++)
        {
            int idx = i;
            this.uiToggle.btn[i].onClick.AddListener(() => {
                this.uiToggle.Init(idx);
            });
        }
    }
}

 


Toggle

PracticeUIToggle

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

public class PracticeUIToggle : MonoBehaviour
{
    [SerializeField]
    private GameObject on;

    [SerializeField]
    private GameObject off;

    public Button[] btn;

    public void Init(int i)
    {
        Debug.Log(i);
        if(i == 0)
        {
            this.Off();
        }
        else
        {
            this.On();
        }
    }

    public void On()
    {
        this.on.SetActive(true);
        this.off.SetActive(false);
    }

    public void Off()
    {
        this.off.SetActive(true);
        this.on.SetActive(false);
    }
}

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

[복습] LearnUGUI - Test04[Stage]  (2) 2023.09.07
[복습] LearnUGUI - Stage  (0) 2023.09.06
[복습] 동기/ 비동기  (0) 2023.08.31
프리팹 parent 변경 실수  (0) 2023.08.30
FindObjectOfType 과 GetComponent의 차이  (0) 2023.08.30