美文网首页
UGUI的鼠标事件

UGUI的鼠标事件

作者: runinnn | 来源:发表于2020-08-18 22:44 被阅读0次

UGUI的鼠标事件:EventTrigger

自己写一个帮助类,便于调用(用回调,对相应鼠标事件添加事件,方便书写)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.EventSystems;

/// <summary>

/// 鼠标事件监听

/// </summary>

public class UIEventListener :  EventTrigger

{

    public delegate void PointerEventDelegate(PointerEventData eventData);

    public PointerEventDelegate PointerEnterEvent;

    public PointerEventDelegate PointerExitEvent;

    public PointerEventDelegate PointerClickEvent;

    public PointerEventDelegate PointerDownEvent;

    public PointerEventDelegate PointerUpEvent;

    public PointerEventDelegate BeginDragEvent;

    public PointerEventDelegate OnDragEvent;

    public PointerEventDelegate EndDragEvent;

    public static UIEventListener GetEventListener(GameObject item)

    {

        UIEventListener uIEvent = null;

        if (item != null)

        {

            uIEvent = item.AddComponent<UIEventListener>();

        }

        return uIEvent;

    }

    /// <summary>

    /// 鼠标划入

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnPointerEnter(PointerEventData eventData)

    {

        base.OnPointerEnter(eventData);

        PointerEnterEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 鼠标移出

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnPointerExit(PointerEventData eventData)

    {

        base.OnPointerExit(eventData);

        PointerExitEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 鼠标点击

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnPointerClick(PointerEventData eventData)

    {

        base.OnPointerClick(eventData);

        PointerClickEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 鼠标抬起

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnPointerDown(PointerEventData eventData)

    {

        base.OnPointerDown(eventData);

        PointerDownEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 鼠标抬起

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnPointerUp(PointerEventData eventData)

    {

        base.OnPointerUp(eventData);

        PointerUpEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 开始拖拽

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnBeginDrag(PointerEventData eventData)

    {

        base.OnBeginDrag(eventData);

        BeginDragEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 拖拽中

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnDrag(PointerEventData eventData)

    {

        base.OnDrag(eventData);

        OnDragEvent?.Invoke(eventData);

    }

    /// <summary>

    /// 拖拽结束

    /// </summary>

    /// <param name="eventData"></param>

    public override void OnEndDrag(PointerEventData eventData)

    {

        base.OnEndDrag(eventData);

        EndDragEvent?.Invoke(eventData);

    }

}

相关文章

网友评论

      本文标题:UGUI的鼠标事件

      本文链接:https://www.haomeiwen.com/subject/dnjadktx.html