공부 기록/유니티 Unity

[Unity/TIL] UI에 바로 적용할 수 있는 기본 제공 이벤트 핸들러 (Hover, Exit, Down, Drag, ...)

톰마토 2025. 3. 24. 23:06

IPointerEnterHandler, IPointerExitHandler 

  • 각각 Hover될 때와 커서가 나갈 때 호출됨. 
  • 인터페이스를 상속받고 구현하면 끝이다.
  • EventSystem + Graphic Raycaster + Canvas 기반 UI에서만 동작하는 이벤트 인터페이스다. 

예시

... 너무 간단

예시

public class ItemSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        // 아이템 정보창 보여주기
        Debug.Log("마우스 호버");
        Inventory.ShowItemDesc(Item.ItemName, Item.ItemDescription);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        // 정보창 닫기
        Debug.Log("마우스 나감");
        Inventory.OffItemDesc();
    }
}

지원되는 이벤트 종류

아래 공식 문서에 종류별로 잘 설명되어있다. 

지원되는 이벤트 종류

https://docs.unity3d.com/kr/2022.1/Manual/SupportedEvents.html

 

지원되는 이벤트 - Unity 매뉴얼

이벤트 시스템은 다수의 이벤트를 지원하며 사용자가 작성한 입력 모듈을 통해 한층 더 효율적으로 커스터마이징할 수 있습니다.

docs.unity3d.com

 

https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.IPointerEnterHandler.html

 

Unity - Scripting API: IPointerEnterHandler

You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see: You've told us there are code samples on this page which don't work. If you know ho

docs.unity3d.com