Unity UI Button实现单击、双击以及长按

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

namespace GeneralTools.UI
{
    public class MultipleButton : Selectable, IEventSystemHandler, ISubmitHandler
    {
        enum ButtonFunction
        {
            OnClickAndLongClick,
            OnClickAndDoubleClick,
            All
        }

        [Header("Multiple Button Config")]

        [SerializeField]
        ButtonFunction buttonFunction = ButtonFunction.All;
        private bool isPress;
        private bool isLongClick;
        [SerializeField]
        private float pressTime = 0.6f;
        private float curTime = 0;

        private bool isDoubleClick;
        private float firstTime = 0;
        [SerializeField]
        private float doubleTime = 0.2f;

        [SerializeField]
        private ButtonClickedEvent onClick;

        public ButtonClickedEvent OnClick
        {
            get => onClick;
            set => onClick = value;
        }

        [SerializeField]
        private ButtonClickedEvent onLongClick;

        public ButtonClickedEvent OnLongClick
        {
            get => onLongClick;
            set => onLongClick = value;
        }

        [SerializeField]
        private ButtonClickedEvent onDoubleClick;

        public ButtonClickedEvent OnDoubleClick
        {
            get => onDoubleClick;
            set => onDoubleClick = value;
        }


        private void Update()
        {
            if (isPress)
            {
                curTime += Time.deltaTime;
            }

            if (firstTime < doubleTime)
            {
                firstTime += Time.deltaTime;
            }

        }

        public void OnSubmit(BaseEventData eventData)
        {
            Debug.Log("Submitted!");
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);

            isPress = true;

            ///双击状态    第二次按下与第一次抬起间隔判断是否触发双击
            if (firstTime <= doubleTime)
            {
                isDoubleClick = true;
            }
            else
            {
                isDoubleClick = false;
            }
        }


        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);

            ///长按状态
            if (curTime>=pressTime&&isPress)
            {
                isLongClick = true;
            }
            else
            {
                isLongClick = false;
            }
            isPress = false;
            curTime = 0;


            ///根据当前状态判断哪种按键触发
            switch (buttonFunction)
            {
                case ButtonFunction.OnClickAndLongClick:
                    if (isLongClick)
                    {
                        Debug.Log("isLongClick");
                        onLongClick?.Invoke();
                    }
                    else
                    {
                        Debug.Log("onClick");
                        onClick?.Invoke();
                    }
                    break;
                case ButtonFunction.OnClickAndDoubleClick:

                    if (isDoubleClick)
                    {
                        Debug.Log("onDoubleClick");
                        onDoubleClick?.Invoke();
                    }
                    else
                    {
                        ///防止双击的同时还触发第一次单击事件
                        StartCoroutine(OnClickButtonEvent());
                    }

                    break;
                case ButtonFunction.All:
                    if (isLongClick)
                    {
                        Debug.Log("isLongClick");
                        onLongClick?.Invoke();
                    }
                    else if (isDoubleClick)
                    {
                        Debug.Log("onDoubleClick");
                        onDoubleClick?.Invoke();
                    }
                    else
                    {
                        StartCoroutine(OnClickButtonEvent());
                    }
                    break;
            }

            ///抬起后重置双击时间
            firstTime = 0;

        }

        IEnumerator OnClickButtonEvent()
        {
            yield return new WaitForSeconds(doubleTime);

            if (!isDoubleClick)
            {
                Debug.Log("onClick");
                onClick?.Invoke();
            }
        }

    }

}

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值