Unity UI common event interface

The UI component itself implements a large number of interfaces, and uses these interfaces to extend its own functions.

Interface naming rules: IxxxHandler.

I: prefix, indicating that it is an interface;

xxx: its own name; IDragHandler[drag handler interface]

Handler: suffix, indicating that it is a handler for a specific function.

Steps for usage:

① When using events in UGUI, you need to introduce a proprietary namespace using UnityEngine.EventSystems in the script;

② After the parent class inherited by the current class, separate with commas, and write the interface name that needs to be used;

③Place the mouse on the interface name, Alt+Enter-->Implementation interface/display implementation interface;

④ Write the method body;

Drag event interface:

IBeginDragHandler : Start dragging event handler; triggered at the moment of dragging.

IDragHandler : Event handler during dragging; triggers continuously during dragging .

IEndDragHandler : End drag event handler; triggered at the end of drag.

The drag event changes the position of the UI picture:

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

public class ItemDrag : MonoBehaviour,IDragHandler,IBeginDragHandler
{
    private RectTransform m_RectTransform;
    Vector3 offset;

    void Start()
    {
        m_RectTransform = gameObject.GetComponent<RectTransform>();
    }

    //                       PointerEventData指针事件数据.
    void IDragHandler.OnDrag(PointerEventData eventData)
    {
        Vector3 pos;
        //RectTransform 工具类.       屏幕坐标点转化为世界坐标点(游戏物体的 RectTransform,当前坐标位置点,         事件摄像机,    最终计算得到的世界坐标位置).
        RectTransformUtility.ScreenPointToWorldPointInRectangle(m_RectTransform, eventData.position, eventData.enterEventCamera, out pos);
        m_RectTransform.position = pos + offset;
    }

    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
        //开始拖拽时计算偏移量.
        offset = m_RectTransform.position - Input.mousePosition;
    }
}

Pointer event interface:

Pointer: It is the mouse pointer on the PC side and the touch screen on the mobile side.

When we move the mouse on the PC side and touch the UI object, the corresponding pointer event will be triggered.

IPointerEnterHandler : The pointer enters the event handler; triggers instantly.

IPointerExitHandler : The pointer leaves the event handler; triggers instantly.

IPointerDownHandler : Pointer down event handler; triggered in an instant.

IPointerUpHandler : Pointer up event handler; triggered instantly.

IPointerClickHandler : Pointer click event handler; pointer down + up = click.

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

public class PointerDemo : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler
{
    //指针单击事件.(显示实现接口是这种格式的)
    void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("OnPointerClick指针单击事件");
    }

    //指针按下事件.
    void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("OnPointerDown指针按下事件");
    }

    //指针进入事件.
    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("OnPointerEnter指针进入事件");
    }

    //指针离开事件.
    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("OnPointerExit指针离开事件");
    }

    //指针抬起事件.(实现接口是这种格式的,效果都一样)
    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("OnPointerUp指针抬起事件");
    }
}

Button button component and Toggle check box component, both of these components have the function of clicking, and their clicking is realized by

The IPointerClickHandler interface is implemented.

These "event interfaces" are like "mechanical parts one by one", and the UI components in UGUI are the "mechanical components" assembled by these parts.

Finished products”. During development, we can also take these “mechanical parts” according to our own functional requirements, and stitch them together to realize our own “mechanical parts”.

finished product".

Event trigger:

The event trigger is equivalent to the Button's event binding panel.

Note: panel binding is more troublesome than code binding, I generally don't use it, just for understanding.

Steps for usage:

① First add an EventTrigger component to the UI object that needs to add an event;

②Click "Add New Event Type" to add a new event;

③ Add the corresponding event processing method in the script, and drag it to the specified event panel position;

④ After binding the script method, the selected event must be consistent with the event of Add New Event Type. [Example in the figure below]

 Script code:

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

public class TriggerDemo : MonoBehaviour
{
    public void PointerEnter(BaseEventData EventData)
    {
        Debug.Log("PointerEnter");
        //强制转换为PointerEventData类型.
        PointerEventData ped = EventData as PointerEventData;
        Debug.Log(ped.position);
    }

    public void PointerExit(BaseEventData EventData)
    {
        Debug.Log("PointerExit");
    }
    public void PointerDown(BaseEventData EventData)
    {
        Debug.Log("PointerDown");
    }
    public void PointerUp(BaseEventData EventData)
    {
        Debug.Log("PointerUp");
    }
    public void PointerClick(BaseEventData EventData)
    {
        Debug.Log("PointerClick");
    }
}

In the way of implementing the interface in the code, the rewritten method parameters are: PointerEventData type, such as  drag event interface, pointer

pin event interface;

But the method of dragging on the EventTrigger event trigger must be of type BaseEventData, or empty;

If you want to use the properties in PointerEventData, you need to perform type conversion (if you need to use position as above, you need

type conversion first).

Guess you like

Origin blog.csdn.net/weixin_55532142/article/details/124820204