해야 할 것

스킬이 중복으로 나오지 않게

그 다음으로 플레이어의 스킬 인벤토리를 만들고

클릭시 순서대로 적용

Panel을 UI로 만든후 시점이 맞았을때

패널을 보이게 하고 그렇지 않을 경우 보이지 않게 한다.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.LowLevel;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private Coroutine coroutine;
    public GameObject menuUI;
    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.Log("Hover");
    }


    public void OnPointerEnter(PointerEventData eventData)
    {
        coroutine = StartCoroutine(CoClick());
        Debug.Log("Enter");
        menuUI.SetActive(true);

    }


    private IEnumerator CoClick()
    {
        float delta = 0f;
        while (true)
        {
            delta += Time.deltaTime;
            if(delta >= 3)
            {
                Debug.LogFormat("Clicked , {0}", delta);
                delta = 0f;
            }
            yield return null;
        }
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Exit");
        menuUI.SetActive(false);
        StopCoroutine(coroutine);
    }
}

 

 

플레이어의 위치에 따라서 패널의 방향이 변경되는데

이는 캔버스안에 넣어주어야 한다.

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

public class Billboard : MonoBehaviour
{
    

    // Update is called once per frame
    void Update()
    {
        this.transform.LookAt(Camera.main.transform.position);
    }
}

 

 

 

 

IPointerEnterHandler, IPointerExitHandler는 유니티엔진에 있는 메소드이지만,

IGvrPointerHoverHandler는 직접 만들어준 메소드이다.

 


 

커스텀 래티클 만들기

 

이번엔 기존에 사용했던 빨간색 래티클을 없애고 새로운 래티클을 만들어 주려 한다.

 

Main Camera의 GvrRecticlePointer와 GvrPointerPhysicsRaycast를 비활성화  한다.

 

Main Camera자식으로 Canvas를 생성하고  다음과  같이  속성을  변경  한다

 

 

그리고 적절한 이미지를 붙여준 후 Fill효과를 넣어줄 것이므로

복사해서 앞과 뒤를 만든다.

Back은 알파 값을 조금 줄여주고

Front는 filled, Radial 360, Top으로 변경하였다.

 

우선 래티클이 변경되었으니 인지를 하게 만드는 것이 우선인데

차의 레이어는 7 메뉴의 레이어는 6으로 만들어서

인지할수 있도록 하였다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.LowLevel;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private static Car instance;
    private Coroutine coroutine;
    public GameObject menuUI;

    private void Start()
    {
        //menuUI.SetActive(false);
    }

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Debug.LogWarning("Duplicate instance of Car found!");
            Destroy(gameObject); // 중복된 인스턴스 제거
        }
    }

    public static Car GetInstance()
    {
        return instance;
    }



    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.Log("Hover");
    }


    public void OnPointerEnter(PointerEventData eventData)
    {
        coroutine = StartCoroutine(CoClick());
        Debug.Log("Enter");
        menuUI.SetActive(true);
        Debug.Log("이벤트 대상: " + eventData.pointerEnter.name);
    }


    private IEnumerator CoClick()
    {
        float delta = 0f;
        while (true)
        {
            delta += Time.deltaTime;
            if(delta >= 3)
            {
                Debug.LogFormat("Clicked , {0}", delta);
                delta = 0f;
            }
            yield return null;
        }
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Exit");
        menuUI.SetActive(false);
        StopCoroutine(coroutine);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class EyeCast : MonoBehaviour
{
    private Transform trans;
    private Ray ray;
    private RaycastHit hit;
    public float dist = 10f;
    public Image gauge;
    bool isOpenedUIMenu = false;
    private Car carInstance; // 전역 변수로 선언

    void Start()
    {
        this.trans = this.GetComponent<Transform>();
        carInstance = Car.GetInstance(); // 전역 변수에 할당
        carInstance.menuUI.SetActive(false);
    }

    void Update()
    {
        ray = new Ray(this.trans.position, this.trans.forward);
        Debug.DrawRay(ray.origin, ray.direction * this.dist, Color.green);

        var mask = 1 << 6 | 1 << 7; // 레이어 마스크 설정
        if (Physics.Raycast(ray, out hit, dist, mask))
        {
            this.GazeButton();
        }
        else
        {
            ReleaseButton();
            // 여기에서 메뉴 닫는 동작을 추가할 수 있습니다.
            // 예: carInstance.menuUI.SetActive(false);
        }
    }

    private void GazeButton()
    {
        if (this.hit.collider.gameObject.layer == 7 && carInstance != null)
        {
            carInstance.menuUI.SetActive(true);
        }
    }

    private void ReleaseButton()
    {
        if (this.hit.collider == null || this.hit.collider.gameObject == null)
        {
            carInstance.menuUI.SetActive(false);
        }
    }
}

 

보기와 같이 코드에서 차뿐만이 아닌 버튼에도 레이어를 설정해두었기 때문에

버튼쪽에 래티클을 갖다 두어도 창이 사라지지 않는다.

 

레이어를 인식해서 게이지를 채우고

나갔을 때 초기화

 

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

public class EyeCast : MonoBehaviour
{
    private Transform trans;
    private Ray ray;
    private RaycastHit hit;
    public float dist = 10f;
    public Image gauge;
    bool isOpenedUIMenu = false;
    private Car carInstance; // 전역 변수로 선언
    private float delta;

    void Start()
    {
        this.trans = this.GetComponent<Transform>();
        carInstance = Car.GetInstance(); // 전역 변수에 할당
        carInstance.menuUI.SetActive(false);
    }

    void Update()
    {
        ray = new Ray(this.trans.position, this.trans.forward);
        Debug.DrawRay(ray.origin, ray.direction * this.dist, Color.green);

        var mask = 1 << 6 | 1 << 7; // 레이어 마스크 설정
        if (Physics.Raycast(ray, out hit, dist, mask))
        { 
            //gauge.fillAmount = 0;
            StartCoroutine(GazeButton());
        }
        else
        {
            delta = 0;
            gauge.fillAmount = 0;

            StartCoroutine(ReleaseButton());
           

            // 여기에서 메뉴 닫는 동작을 추가할 수 있습니다.
            // 예: carInstance.menuUI.SetActive(false);
        }
    }
    IEnumerator GazeButton()
    {
        
        if (this.hit.collider.gameObject.layer == 7 && carInstance != null)
        {
            carInstance.menuUI.SetActive(true);

            this.delta += Time.deltaTime;
            gauge.fillAmount = delta / 3;
        }
        else
        {

        }
        yield return null;
    }

    IEnumerator ReleaseButton()
    {
        if (this.hit.collider == null || this.hit.collider.gameObject == null)
        {

            carInstance.menuUI.SetActive(false);


        }
        yield return null;
    }
}

 

 

게이지를 채워서 예를 누르면 예가 찍히고

아니요를 누르면 창을 종료 시켰다.

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

public class EyeCast : MonoBehaviour
{
    private Transform trans;
    private Ray ray;
    private RaycastHit hit;
    public float dist = 10f;
    public Image gauge;
    private Car carInstance; // 전역 변수로 선언
    private float delta;
    public Button clickYes;
    public Button clickNo;
    private bool isYesClicked = false;
    private Coroutine coroutine;

    void Start()
    {
        this.trans = this.GetComponent<Transform>();
        carInstance = Car.GetInstance(); // 전역 변수에 할당
        carInstance.menuUI.SetActive(false);
    }

    void Update()
    {
        

        ray = new Ray(this.trans.position, this.trans.forward);
        Debug.DrawRay(ray.origin, ray.direction * this.dist, Color.green);

        var mask = 1 << 7; // 레이어 마스크 설정
        if (Physics.Raycast(ray, out hit, dist, mask))
        {
            //gauge.fillAmount = 0;
            
              if(coroutine != null)
            {
                StopCoroutine(coroutine);

            }

            StartCoroutine(GazeButton());

        }
       

        else if (Physics.Raycast(ray, out hit, dist, 1 << 6))
        {
            //gauge.fillAmount = 0;
            //StartCoroutine(GazeButton());
       
         
              
                StartCoroutine(GazeButton());
                StartCoroutine(ClickButton());

          
           
        }

        else
        {
            if (isYesClicked == true)
            {
                isYesClicked = false;
            }
            delta = 0;
            gauge.fillAmount = 0;

            StartCoroutine(ReleaseButton());


            // 여기에서 메뉴 닫는 동작을 추가할 수 있습니다.
            // 예: carInstance.menuUI.SetActive(false);
        }
    }
    IEnumerator GazeButton()
    {

        if (this.hit.collider.gameObject.layer == 7 || this.hit.collider.gameObject.layer == 6 && carInstance != null)
        {
            carInstance.menuUI.SetActive(true);

            this.delta += Time.deltaTime;
            gauge.fillAmount = delta / 2;
        }
        else
        {
            delta = 0;
            gauge.fillAmount = 0;
        }
        yield return null;
    }

    IEnumerator ClickButton()
    {
        if (this.hit.collider.gameObject.layer == 6)
        {
            this.delta += Time.deltaTime;
            if (gauge.fillAmount >= 1)
            {
                if (hit.collider.gameObject.name == "ButtonYes" && isYesClicked == false)
                {
                    isYesClicked = true;
                    Debug.Log("예를 입력하였습니다");
                }
                if (hit.collider.gameObject.name == "ButtonNo")
                {
                    carInstance.menuUI.SetActive(false);
                }
            }
        }
        yield return null;
    }


    IEnumerator ReleaseButton()
    {
        if (this.hit.collider == null || this.hit.collider.gameObject == null)
        {
            carInstance.menuUI.SetActive(false);
        }
        yield return null;
    }
}

 

 

 

마지막으로 레이어 이동시 게이지를 초기화 시키도록 구현하기 위해 수정을 하였다.

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

public class EyeCast : MonoBehaviour
{
    private Transform trans;
    private RaycastHit hit;
    public float dist = 10f;
    public Image gauge;
    public Car car;
    public Button clickYes;
    public Button clickNo;

    private int lastLayer = 0;
    private string lastButton = "";

    float carCount = 0;
    float yesButtonCount = 0;
    float noButtonCount = 0;


    void Start()
    {
        this.trans = this.GetComponent<Transform>();
        car.menuUI.SetActive(false); // 처음엔 menu가 닫힌채로 시작

        StartCoroutine(ReleaseButton());
        StartCoroutine(GazeButton());
        StartCoroutine(ClickButton());
    }


    IEnumerator GazeButton()
    {
        while (true)
        {
            Ray ray = new Ray(this.trans.position, this.trans.forward);
            if (Physics.Raycast(ray, out hit, dist, 1 << 7)) // Layer 7 = Car
            {
                if (lastLayer != 7 && carCount < 2)
                {
                    carCount = 0;
                    gauge.fillAmount = 0;
                }
                else if (lastLayer != 7 && carCount >= 2)
                {
                    carCount = 2;
                    gauge.fillAmount = 1;
                }
                else if (lastLayer !=7 && carCount >= 2 && !car.menuUI.gameObject.activeSelf) // No 버튼으로 menu가 닫혔을때, 다시 초기화
                {
                    carCount = 0;
                    gauge.fillAmount = 0;
                }
                lastLayer = 7;
                lastButton = "";

                //car.menuUI.SetActive(true);  -> Car를 응시 했을 때, 바로 메뉴 오픈

                if (carCount < 2)
                {
                    carCount += Time.deltaTime;
                    gauge.fillAmount = carCount / 2;

                    if (gauge.fillAmount >= 1)
                    {
                        car.menuUI.SetActive(true); // -> Car를 응시 했을 때, 2초 후 메뉴 오픈
                    }
                }
            }
            yield return null;
        }
    }



    IEnumerator ClickButton()  // ButtonYes와 ButtonNo 콜라이더 사이에 공백이 있으면 ResetCountAndGauge 메서드(메뉴 닫힘 및 초기화)가 실행 될 수 있으니 유의
    {
        while (true)
        {
            Ray ray = new Ray(this.trans.position, this.trans.forward);
            if (Physics.Raycast(ray, out hit, dist, 1 << 6)) //Layer 6 = Button
            {
                if (hit.collider.gameObject.name == "ButtonYes")
                {
                    lastLayer = 6;

                    if (lastButton != "ButtonYes" && yesButtonCount < 2)
                    {
                        lastButton = "ButtonYes";
                        yesButtonCount = 0;
                        gauge.fillAmount = 0;
                    }
                    else if (lastButton != "ButtonYes" && yesButtonCount >= 2)
                    {
                        lastButton = "ButtonYes";
                        yesButtonCount = 2;
                        gauge.fillAmount = 1;
                    }
                    if (yesButtonCount < 2)
                    {
                        yesButtonCount += Time.deltaTime;
                        gauge.fillAmount = yesButtonCount / 2;

                        if (gauge.fillAmount >= 1)
                        {
                            Debug.Log("예를 입력하였습니다");
                            car.uiText.text = "차량 정보 확인"; // UI Text 변경
                        }
                    }
                }
                else if (hit.collider.gameObject.name == "ButtonNo")
                {
                    lastLayer = 6;

                    if (lastButton != "ButtonNo")
                    {
                        lastButton = "ButtonNo";
                        noButtonCount = 0;
                        gauge.fillAmount = 0;
                    }
                    if (noButtonCount < 2)
                    {
                        noButtonCount += Time.deltaTime;
                        gauge.fillAmount = noButtonCount / 2;

                        if (gauge.fillAmount >= 1)
                        {
                            car.menuUI.SetActive(false);

                            // Count 및 Text 초기화
                            car.uiText.text = "차량의 정보를 보시겠습니까?"; 
                            carCount = 0;
                            yesButtonCount = 0;
                            noButtonCount = 0;
                            Debug.Log("아니오를 입력하였습니다. 메뉴를 종료합니다.");
                        }
                    }
                }
            }
            yield return null;
        }
    }


    IEnumerator ReleaseButton()
    {
        while (true)
        {
            Ray ray = new Ray(this.trans.position, this.trans.forward);
            if (!Physics.Raycast(ray, out hit, dist))
            {
                //car.menuUI.SetActive(false); -> Car, 버튼 어느것도 응시하지 않으면 menu 닫음
                gauge.fillAmount = 1;
                lastLayer = 0;
                lastButton = "";
            }
            yield return null;
        }
    }





}

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.UI;
using TMPro;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{

    private Coroutine coroutine;
    public GameObject menuUI;
    public Text uiText; // menuUI Text

    private void Start()
    {
        //menuUI.SetActive(false);
    }



    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.Log("Hover");
    }


    public void OnPointerEnter(PointerEventData eventData)
    {
        //coroutine = StartCoroutine(CoClick());
        //Debug.Log("Enter");
        ////menuUI.SetActive(true);
        //Debug.Log("이벤트 대상: " + eventData.pointerEnter.name);
    }


    private IEnumerator CoClick()
    {
        float delta = 0f;
        while (true)
        {
            delta += Time.deltaTime;
            if(delta >= 3)
            {
                Debug.LogFormat("Clicked , {0}", delta);
                delta = 0f;
            }
            yield return null;
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        //Debug.Log("Exit");
        //menuUI.SetActive(false);
        //StopCoroutine(coroutine);
    }
}

구조

이때 주의할것은 뭐가 위에 올라와야 하는지 구조를 잡는 것이다.

 

 

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

public class UISkillButton : MonoBehaviour
{
    [SerializeField] private Button btn;
    [SerializeField] private int maxCooltime = 10;
    [SerializeField] private TMP_Text cooltimeText;
    [SerializeField] private Image slier;
    private float cooltime;
    private bool isCoolTime;

    void Start()
    {
        btn.onClick.AddListener(() => {
            if (IsAvaliable())
            {
                this.UseSkill();
            }
            else
            {
                Debug.Log("아직은 사용할수 없습니다.");
            }
        });

        this.Init();
    }

    private void Init()
    {
        Debug.Log("스킬버튼을 초기화 합니다.");
        this.cooltime = this.maxCooltime;
        this.slier.fillAmount = 0;
        this.cooltimeText.text = this.cooltimeText.ToString();
        this.cooltimeText.gameObject.SetActive(false);
        this.isCoolTime = false;
    }

    private void UseSkill()
    {
        Debug.Log("스킬을 사용했습니다.");
        this.cooltimeText.gameObject.SetActive(true);
        this.cooltimeText.text = this.cooltime.ToString();
        this.slier.fillAmount = 1;
        this.StartCoroutine(this.CoWaitForCoolTime());
    }

    private IEnumerator CoWaitForCoolTime()
    {
        this.isCoolTime = true;

        while (true)
        {
            this.cooltime -= Time.deltaTime;
            this.cooltimeText.text = Mathf.RoundToInt(this.cooltime).ToString();
            this.slier.fillAmount = this.cooltime / this.maxCooltime;
            if (this.cooltime <= 0)
            {
                break;
            }
            yield return null;
        }

        this.isCoolTime = false;

        Debug.Log("스킬을 사용할수 있습니다.");

        this.Init();
    }

    private bool IsAvaliable()
    {
        return !isCoolTime;
    }
}

 

 

'낙서장 > UIUX' 카테고리의 다른 글

Slider  (0) 2024.03.18
Switch Button  (0) 2024.03.18
Button and Inputfield  (0) 2024.03.18

우선 한 버튼을 누를때마다 동작을 변화시킬 것이므로

캔버스에 빈 버튼안에 on, off로 나누었다.

텍스트는 보이지 않게 이미지를 껐다.

 

자리를 잘 잡아주어 이렇게 분류한다.

기본을 켜져있을 때로 주었고

눌렀을 때 !을 사용해서 서로의 이미지를 교차로 보일 수 있게 하였다.

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

public class Switch : MonoBehaviour
{
    [SerializeField] private Button btnSwitch;
    [SerializeField] private GameObject btnOn;
    [SerializeField] private GameObject btnOff;
    private bool isOn = true;

    private void Start()
    {
        btnOn.SetActive(isOn);
        btnOff.SetActive(!isOn);
        
    }

    private void Update()
    {
        this.btnSwitch.onClick.AddListener(() => {
            OnOff();
        });
    }

    void OnOff()
    {
        isOn = !isOn;
        btnOn.SetActive(isOn);
        btnOff.SetActive(!isOn);
    }

}

https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

 

Unity - Scripting API: GameObject.SetActive

A GameObject may be inactive because a parent is not active. In that case, calling SetActive will not activate it, but only set the local state of the GameObject, which you can check using GameObject.activeSelf. Unity can then use this state when all paren

docs.unity3d.com

 

 

 

'낙서장 > UIUX' 카테고리의 다른 글

Skill Button  (0) 2024.03.18
Slider  (0) 2024.03.18
Button and Inputfield  (0) 2024.03.18

우선 가이드 라인을 캔버스에 적용한후

플레이하는 기계에 따라 유동적으로 변화할 수 있게

스케일모드와 레퍼런스 솔루션을 적용

 

 

알베도를 설정하여 가이드라인을 흐리게 설정

 

데모씬으로 들어가서 오브젝트들을 하나씩 활성화 시켜본 후 필요한 것을 찾고

그 안에있는 컴포넌트들을 확인해야 한다.

그리고 이런 느낌으로 메모장에 적어둔다.

버튼을 만들것인데 TMP로 만들것이다.

https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.0/api/TMPro.TextMeshProUGUI.html

 

Class TextMeshProUGUI

Class TextMeshProUGUI Inherited Members Namespace: TMPro Assembly : solution.dll Syntax public class TextMeshProUGUI : TMP_Text, ILayoutElement Fields m_subTextObjects Declaration protected TMP_SubMeshUI[] m_subTextObjects Field Value Properties autoSizeTe

docs.unity3d.com

 

아까 메모장에 적어뒀던걸로 검색해서 이런식으로 assign

 

가져오면 이런식으로 sliced가 되있을 것인데 사이즈를 바꿔줄 것이다.

이러면 기본적인 크기가 조절되는데 다시 sliced로 바꿔야 이미지 크기를 조절할때

이미지가 찌그러지는 상황을 방지할 수 있다.

 

UI를 만들때 기초이자 필수인 과정인데

가이드란과 비교해서 그 위해 이미지를 덮고 이미지와 텍스트를껐다켰다하며 실제와 같은지 비교하고 조정한다.

조정할 것으로는 이미지크기, 텍스트, 색상 같은 것들이 있다.

UI들을 관리해줄 빈오브젝트 생성 후 스크립트 어싸인

 

이런식으로 직접 할당해줄수 있지만 나중에 컴포넌트들이 많으면 관리하기가 힘들고

혹시나 나중에 디자이너들과 협업을 할때 쉽게 건들여질 수 있는 부분으로 권장하지 않는다.

따라서 초기화

이런식으로 인스펙터에서 할당하지 않아도 클릭하면 인지될 수 있도록 코드를 구현할 것이다.

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

public class BlueButton : MonoBehaviour
{
    [SerializeField] public Button blueButton;

    private void Start()
    {
        this.blueButton.onClick.AddListener(() => {
            Debug.Log("Blue Button Clicked!");
        });
    }
}

 

이제 Inputfield를 만들 것인데 버튼과 마찬가지로 필요한 텍스쳐들을 찾아낸다.

칼라도 잊지말고 찾자

Allignment는 사실 크게 신경쓰지 않아도 된다.

위에 있는 것들은 버튼을 만들때 필요했던 것들 아래는 이번에 만들 인풋필드에 필요한 것들이다.

.

기존에 인풋필드에서 사용하는 폰트는 한글이 없으므로 설치후

window - textmeshpro - font asset creator

글자는 각 숫자로 정리 되어있는데 이것은 한글 범위를 찾아서 넣어줘고 제너레이트 해야 한다.

 

https://docs.unity3d.com/Packages/com.unity.textmeshpro@4.0/manual/FontAssetsCreator.html

 

Font Asset Creator | TextMeshPro | 4.0.0-pre.2

Font Asset Creator The Font Asset Creator converts Unity font assets into TextMesh Pro font assets. You can use it to create both Signed Distance Field (SDF) fonts and bitmap fonts. When you create a new font Asset, TextMesh Pro generates the Asset itself,

docs.unity3d.com

제너레이트 후 세이브

 

아까는 onClick()를 썼듯이 이번엔 이것을 사용할 것이다.

어싸인

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

public class InputField : MonoBehaviour
{
    [SerializeField] public TMP_InputField inputField;
    void Start()
    {
        this.inputField.onValueChanged.AddListener((word) =>
        {
            Debug.LogFormat("{0}", word);
        });
    }
}

이제 입력을 하고 버튼을 누르면 출력되게 할 것이다.

using UnityEngine;
using UnityEngine.UI;

public class BlueButton : MonoBehaviour
{
    public Button blueButton;
    private ShowText showText;

    void Start()
    {
        this.showText = GetComponent<ShowText>();
        blueButton.onClick.AddListener(() =>
        {
            showText.ShowMessage();
        });
    }
}

 

using TMPro;
using UnityEngine;

public class ShowText : MonoBehaviour
{
    public TMP_InputField inputField;

    public void ShowMessage()
    {
        Debug.Log(inputField.text);
    }

    //public void SubscribeToInputFieldChange()
    //{
    //    inputField.onValueChanged.AddListener((word) =>
    //    {
    //    });
    //}
}

'낙서장 > UIUX' 카테고리의 다른 글

Skill Button  (0) 2024.03.18
Slider  (0) 2024.03.18
Switch Button  (0) 2024.03.18

기존에 있는 에셋을 다운받아

가이드라인으로 잡고 명암을 줄이고

그 에셋을 찾아 UI를 본뜨는 작업을 하였는데

그것을 하기 전에 제일 기초이자 중요한 GameObject.onClick.AddListener()를 다루려 한다.

 

Button1을 클릭하면 콘솔에 찍힌다.

 

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

public class Main : MonoBehaviour
{
    [SerializeField] private Button btn;

    void Start()
    {
        btn.onClick.AddListener(() =>
        {
            Debug.Log("button clicked!");
        });
    }
};

 

Button1을 클릭하면 2, 3번이 사라진다.

 

 

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

public class Main : MonoBehaviour
{
    [SerializeField] private Button btn1;

    [SerializeField] private Button btn2;
    [SerializeField] private Button btn3;

    void Start()
    {
        btn1.onClick.AddListener(() => {
            this.btn2.gameObject.SetActive(false);
            this.btn3.gameObject.SetActive(false);
        });
    }
}

 

Button1을 클릭하면 2, 3이 사라지는데

Button2, Butto3을 배열(btns)로 만들어서 할당하였다.

 

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

public class Main : MonoBehaviour
{
    [SerializeField] private Button btn1;

    [SerializeField] private Button[] btns;

    void Start()
    {
        btn1.onClick.AddListener(() => {

            for (int i = 0; i < this.btns.Length; i++)
            {
                Button btn = btns[i];
                btn.gameObject.SetActive(false);
            }
        });
    }
}

 

Button1, Button2를 btns(배열)에 할당하였고

Button3을 누를시 배열이 사라지고 Button4를 누를시 배열이 다시 생기게 작성

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

public class Main : MonoBehaviour
{
    [SerializeField] private Button btn3;
    [SerializeField] private Button btn4;
    [SerializeField] private Button[] btns; //btn1, btn2

    void Start()
    {
        btn3.onClick.AddListener(() =>
        {
            Debug.Log("버튼3 눌렸습니다.");
            for (int i = 0; i < this.btns.Length; i++)
            {
                Button btn = btns[i];
                btn.gameObject.SetActive(false);
            }
        });

        btn4.onClick.AddListener(() =>
        {
            Debug.Log("버튼4 눌렸습니다.");
            for (int i = 0; i < this.btns.Length; i++)
            {
                Button btn = btns[i];
                btn.gameObject.SetActive(true);
            }
        });
    }
}

'산대특 > 게임 알고리즘' 카테고리의 다른 글

AppleCatch  (0) 2024.02.06
동기와 비동기  (1) 2024.02.05
직렬화와 역직렬화  (0) 2024.02.05
디자인 패턴과 싱글톤 패턴  (0) 2024.02.05
Process 와 Thread 그리고 Thread 와 Coroutine  (1) 2024.02.04

+ Recent posts