SDHK_Tool.Component.SC_TouchEvent_Down 点击事件监听器

 

* 作者:闪电Y黑客

* 日期: 2019.7.16

* 功能:点击事件监听

 

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using SDHK_Tool.Dynamic;

/*
 * 作者:闪电Y黑客
 * 
 * 日期: 2019.7.16
 * 
 * 功能:点击事件监听
 *
 */


namespace SDHK_Tool.Component
{
    /// <summary>
    /// 触摸事件监听器:【点击事件】
    /// </summary>
    public class SC_TouchEvent_Down : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IBeginDragHandler, IEndDragHandler
    {
        [Tooltip("忽略鼠标")]
        public bool IgnoreMouse = false;//忽略鼠标

        [Tooltip("忽略拖拽事件影响")]
        public bool IgnoreDrag = true;	//忽略拖拽

        [Tooltip("触摸时间")]
        public float TouchTime = 0.5f;	//触摸时间

        /// <summary>
        /// 触摸事件:按下
        /// </summary>
        public Action TouchOnDown;      

        /// <summary>
        /// 触摸事件:长按按下
        /// </summary>
        public Action TouchOnLongDown;	

        /// <summary>
        /// 触摸事件:按住【Update】
        /// </summary>
        public Action TouchOnStay;		

        /// <summary>
        /// 触摸事件:短按按住【Update】
        /// </summary>
        public Action TouchOnShortStay;	

        /// <summary>
        /// 触摸事件:长按按住【Update】
        /// </summary>
        public Action TouchOnLongStay;	



        /// <summary>
        /// 触摸事件:按下后抬起
        /// </summary>
        public Action TouchOnUp;		

        /// <summary>
        /// 触摸事件:短按后抬起
        /// </summary>
        public Action TouchOnShortUp;	

        /// <summary>
        /// 触摸事件:长按后抬起
        /// </summary>
        public Action TouchOnLongUp;   



        /// <summary>
        /// 触摸事件:点击
        /// </summary>
        public Action TouchOnClick;     

        /// <summary>
        /// 触摸事件:短点击
        /// </summary>
        public Action TouchOnShortClick;

        /// <summary>
        /// 触摸事件:长点击
        /// </summary>
        public Action TouchOnLongClick; 


        private int touchCount = 0;		//触摸数量
        public int TouchCount { get { return touchCount; } }


        private SD_MarkerClock MarkerClock;

        private bool isDown = false;     //已按下

        private bool isTouch = true;

        private bool isLongDown = false; //长按触发


        void Start()
        {
            MarkerClock = new SD_MarkerClock();
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (IgnoreMouse && eventData.pointerId < 0) return;
            touchCount++;
            if (touchCount == 1)
            {
                isDown = true;
                MarkerClock.Reset_Marker();
            }

            if (TouchOnDown != null) TouchOnDown();
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (IgnoreMouse && eventData.pointerId < 0) return;
            touchCount--;

            if (TouchOnUp != null) TouchOnUp();

            if (touchCount == 0 && isTouch)
            {
                if (!isLongDown && TouchOnShortUp != null) TouchOnShortUp();
                if (isLongDown && TouchOnLongUp != null) TouchOnLongUp();
            }

            if (touchCount == 0)
            {
                isDown = false;
                isLongDown = false;
            }

        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (IgnoreMouse && eventData.pointerId < 0) return;

            if (TouchOnClick != null) TouchOnClick();

            if (touchCount == 0 && isTouch)
            {
                if (!isLongDown && TouchOnShortClick != null) TouchOnShortClick();
                if (isLongDown && TouchOnLongClick != null) TouchOnLongClick();
            }

        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (!IgnoreDrag) isTouch = false;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            isTouch = true;
        }


        void Update()
        {
            if (isDown && TouchOnStay != null) TouchOnStay();

            if (isDown && isTouch)
            {
                if (MarkerClock.isClock(TouchTime))
                {
                    isLongDown = true;
                    if (TouchOnLongDown != null) TouchOnLongDown();
                }

                if (!isLongDown && TouchOnShortStay != null) TouchOnShortStay();
                if (isLongDown && TouchOnLongStay != null) TouchOnLongStay();
            }

        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值