[Unity] EventSystem으로 Drag 이벤트 받기

Yerin·2023년 7월 24일
0

UI_EventHandler라는 이름의 스크립트를 생성한 뒤,
Image 오브젝트에 붙여준다.

아래와 같이 BeginDrag 와 Drag 시 메세지를 출력하도록 테스트를 한다.
또한 드래그를 한 위치로 이미지를 이동시키는 코드를 작성한다.

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

public class UI_EventHandler : MonoBehaviour, IBeginDragHandler, IDragHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
        Debug.Log("OnDrag");
    }
}

profile
재밌는 코딩 공부

1개의 댓글

comment-user-thumbnail
2023년 7월 24일

잘 봤습니다. 좋은 글 감사합니다.

답글 달기