Detailed explanation of Event Trigger (unity)

Event Trigger for detailed explanation (in unity)

introduce

Event Trigger is a component in Unity, which is used to trigger various events on UI elements, such as click, drag, enter, etc. It provides a simple and flexible way to respond to and handle user input events. Event Trigger can be associated with different event types, and each event type can be associated with multiple callback functions.

method

The Event Trigger component has the following common methods:

  1. OnPointerEnter: Event fired when the mouse pointer enters the UI element.
  2. OnPointerExit: Event fired when the mouse pointer leaves the UI element.
  3. OnPointerDown: Event triggered when the mouse presses the UI element.
  4. OnPointerUp: Event triggered when the mouse lifts the UI element.
  5. OnPointerClick: Event triggered when the mouse clicks on the UI element.
  6. OnDrag: Event triggered when the mouse drags the UI element.
  7. OnDrop: Event triggered when the mouse is dragged from one UI element to another UI element.
  8. OnScroll: Event fired when the mouse wheel is scrolled.

These methods can be configured in the Inspector panel of the Event Trigger component. You can click the "Add New Event Type" button to add different event types and associate corresponding callback functions.

for example

Here are a few common code examples showing how to use the Event Trigger component:

  1. Detect mouse entering UI element and play animation:
using UnityEngine;
using UnityEngine.EventSystems;

public class ButtonAnimation : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    
    
    public Animator animator;

    public void OnPointerEnter(PointerEventData eventData)
    {
    
    
        animator.SetBool("IsHovered", true);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
    
    
        animator.SetBool("IsHovered", false);
    }
}
  1. Detect mouse clicks on UI elements and trigger events:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ButtonClick : MonoBehaviour, IPointerClickHandler
{
    
    
    public Text text;

    public void OnPointerClick(PointerEventData eventData)
    {
    
    
        text.text = "Button Clicked!";
    }
}
  1. Detect drag and drop events of UI elements:
using UnityEngine;
using UnityEngine.EventSystems;

public class DragElement : MonoBehaviour, IDragHandler, IEndDragHandler
{
    
    
    public RectTransform targetRectTransform;

    public void OnDrag(PointerEventData eventData)
    {
    
    
        targetRectTransform.position = eventData.position;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
    
    
        // 处理拖拽结束的逻辑
    }
}

These examples demonstrate how to use the Event Trigger component to listen to different events and handle the events through corresponding callback functions. You can extend and customize these codes according to your needs.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/131446597