| 123456789101112131415161718192021222324252627282930313233343536373839 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- using System;
- public class LongPressMonitor : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
- {
- public Action onLongPress;
- public float interval = 1.0f;
- bool isDown;
- float downTime;
- [NonSerialized] public bool isLongPress; //最后一次点击是否为长按
- void Update()
- {
- if (isDown)
- {
- if (Time.time - downTime > interval)
- {
- isDown = false;
- isLongPress = true;
- onLongPress?.Invoke();
- }
- }
- }
- public void OnPointerDown(PointerEventData eventData)
- {
- isDown = true;
- isLongPress = false;
- downTime = Time.time;
- }
- public void OnPointerExit(PointerEventData eventData)
- {
- isDown = false;
- }
- public void OnPointerUp(PointerEventData eventData)
- {
- isDown = false;
- }
- }
|