SDHK_Tool.Component.SC_TouchEvent_Enter 经过事件监听器

 

* 作者:闪电Y黑客

* 日期: 2019.7.16

* 功能:经过事件监听

 

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


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

namespace SDHK_Tool.Component
{
    /// <summary>
    /// 触摸事件监听器:【经过事件】
    /// </summary>
    public class SC_TouchEvent_Enter : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {

        [Tooltip("忽略鼠标")]
        public bool IgnoreMouse = false;//忽略鼠标

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

        /// <summary>
        /// 触摸事件:进入停留
        /// </summary>
        public Action TouchOnEnter;         //进入

        /// <summary>
        /// 触摸事件:进入长停留
        /// </summary>
        public Action TouchOnLongEnter;     //进入停留

        /// <summary>
        /// 触摸事件:进入停留【Update】
        /// </summary>
        public Action TouchOnInside;        //停留【Update】

        /// <summary>
        /// 触摸事件:进入短停留【Update】
        /// </summary>
        public Action TouchOnShortInside;   //短停留【Update】

        /// <summary>
        /// 触摸事件:进入长停留【Update】
        /// </summary>
        public Action TouchOnLongInside;    //长停留【Update】

        /// <summary>
        /// 触摸事件:离开
        /// </summary>
        public Action TouchOnExit;        //离开

        /// <summary>
        /// 触摸事件:短停留后离开
        /// </summary>
        public Action TouchOnShortExit;   //短停留后离开

        /// <summary>
        /// 触摸事件:长停留后离开
        /// </summary>
        public Action TouchOnLongExit;    //长停留后离开

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

        private SD_MarkerClock MarkerClock;

        private bool isEnter = false;     //已进入

        private bool isLongEnter = false; //长停留触发


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


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

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

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

            if (TouchOnExit != null) TouchOnExit();

            if (touchCount == 0)
            {
                if (!isLongEnter && TouchOnShortExit != null) TouchOnShortExit();
                if (isLongEnter && TouchOnLongExit != null) TouchOnLongExit();
            }

            if (touchCount == 0)
            {
                isEnter = false;
                isLongEnter = false;
            }
        }

        void Update()
        {

            if (isEnter)
            {
                if (TouchOnInside != null) TouchOnInside();

                if (MarkerClock.isClock(TouchTime))
                {
                    isLongEnter = true;
                    if (TouchOnLongEnter != null) TouchOnLongEnter();
                }

                if (!isLongEnter && TouchOnShortInside != null) TouchOnShortInside();
                if (isLongEnter && TouchOnLongInside != null) TouchOnLongInside();
            }
        }
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值