본문 바로가기
Unity/기초

클릭, 터치 상태에 따른 이벤트

by 롤다 2024. 2. 8.

https://docs.unity3d.com/kr/2019.3/ScriptReference/EventSystems.IPointerClickHandler.html

 

EventSystems.IPointerClickHandler - Unity 스크립팅 API

Interface to implement if you wish to receive OnPointerClick callbacks.

docs.unity3d.com

using UnityEngine.EventSystems;

public class DragHandler : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        // 클릭
    }
}
// 스크롤이 있는곳에서 누름상태에서 드래그 적용할 때
public class Drag : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
    public ScrollRect scrollView;
    
    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
        if (isBttDownCool)
        {
            // 드래그 시작
        }
        else
        {
        	// 스크롤 적용
            scrollView.OnBeginDrag(eventData);
        }
    }

    void IDragHandler.OnDrag(PointerEventData eventData)
    {
        if (isBttDownCool)
        {
            // 드래그 중
        }
        else
        {
        	// 스크롤 적용
            scrollView.OnDrag(eventData);
        }
    }

    void IEndDragHandler.OnEndDrag(PointerEventData eventData)
    {
        if (isBttDownCool)
        {
            // 드래그 끝
        }
        else
        {
        	// 스크롤 적용
            scrollView.OnEndDrag(eventData);
        }
    }
    public bool isBttDownCheck;
    public bool isBttDownCool;
    WaitForFixedUpdate BttDownCoolCoroutineRe = new();
    IEnumerator BttDownCoolCoroutine;
    public void BttDownCoolStart()
    {
        isBttDownCheck = true;
        if (BttDownCoolCoroutine != null)
        {
            StopCoroutine(BttDownCoolCoroutine);
        }
        BttDownCoolCoroutine = BttDownCoolCool(0.2f);
        StartCoroutine(BttDownCoolCoroutine);
    }
    IEnumerator BttDownCoolCool(float cool)
    {
        while (cool >= 0f)
        {
            cool -= Time.deltaTime;
            yield return BttDownCoolCoroutineRe;
        }
        if (isBttDownCheck)
        {
            isBttDownCool = true;
            button.enabled = false;
        }
    }
    // 마우스 뗄 때 따로 인스펙터에서 설정해줘야 함
    public void BttUp()
    {
        isBttDownCheck = false;
        if (!isBttDownCool)
        {
            button.enabled = true;
        }
        else if(!dragCheck)
        {
            isBttDownCool = false;
        }
    }
}

'Unity > 기초' 카테고리의 다른 글

Layer, SoftLayer, Tag  (0) 2024.02.08
List, Dictionary  (0) 2024.02.08
Move, Rotation, Quaternion  (0) 2024.02.08
Physics Joints in Unity 2D(물리관절)  (0) 2024.02.08
Raycast, Physics, Rigidbody  (0) 2024.02.08