【Unity】UI点击事件处理器

发布于:2025-04-20 ⋅ 阅读:(86) ⋅ 点赞:(0)

目录

前言

在开发过程中,经常需要监听UI的点击事件,这里我给大家整理一下,脚本直接挂在需要监听的节点上即可。

脚本

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace GameLogic
{
    /// <summary>
    /// Ui点击事件处理器
    /// </summary>
    public class UIEventHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler
    {

        UnityAction enterEvent;
        UnityAction exitEvent;
        UnityAction clickEvent;
        UnityAction downEvent;
        UnityAction upEvent;


        public void Init()
        {
            enterEvent = null;
            exitEvent = null;
            clickEvent = null;
            downEvent = null;
            upEvent = null;
        }

        /// <summary>
        /// 添加进入事件
        /// </summary>
        /// <param name="action"></param>
        public void AddEnterEvent(UnityAction action)
        {
            enterEvent = action;
        }

        /// <summary>
        /// 添加离开事件
        /// </summary>
        /// <param name="action"></param>
        public void AddExitEvent(UnityAction action)
        {
            exitEvent = action;
        }

        /// <summary>
        /// 添加点击事件
        /// </summary>
        /// <param name="action"></param>
        public void AddClickEvent(UnityAction action)
        {
            clickEvent = action;
        }

        /// <summary>
        /// 添加按下事件
        /// </summary>
        /// <param name="action"></param>
        public void AddDownEvent(UnityAction action)
        {
            downEvent = action;
        }

        /// <summary>
        /// 添加松开事件
        /// </summary>
        /// <param name="action"></param>
        public void AddUpEvent(UnityAction action)
        {
            upEvent = action;
        }


        /// <summary>
        /// 进入范围触发
        /// </summary>
        /// <param name="eventdata"></param>
        public void OnPointerEnter(PointerEventData eventdata)
        {
            //Debug.Log("===进入===");
            enterEvent?.Invoke();

        }

        /// <summary>
        /// 退出范围触发
        /// </summary>
        /// <param name="eventdata"></param>
        public void OnPointerExit(PointerEventData eventdata)
        {
            //Debug.Log("===离开===");
            exitEvent?.Invoke();
        }

        /// <summary>
        /// 按下+松开才能触发(松开时需要在UI范围内,不然则不触发)
        /// </summary>
        /// <param name="eventdata"></param>
        public void OnPointerClick(PointerEventData eventdata)
        {
            //Debug.Log("===点击===");
            clickEvent?.Invoke();
        }

        /// <summary>
        /// 按下触发
        /// </summary>
        /// <param name="eventdata"></param>
        public void OnPointerDown(PointerEventData eventdata)
        {
            //Debug.Log("===按下===");
            downEvent?.Invoke();
        }

        /// <summary>
        /// 松开触发
        /// </summary>
        /// <param name="eventdata"></param>
        public void OnPointerUp(PointerEventData eventdata)
        {
            //Debug.Log("===抬起===");
            upEvent?.Invoke();
        }


    }
}


网站公告

今日签到

点亮在社区的每一天
去签到