using System; using System.Collections; using System.Collections.Generic; using Fort23.UTool; using UnityEngine; using UnityEngine.EventSystems; public class LongPressBtn : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler { private float _time; public float startTime = 0.5f; public float intervalTime = 0.05f; public Action onClick; /// /// 连按 /// public Action longPress; /// /// 长按结束 /// public Action longPressEnd; /// /// 是否按下状态 /// [HideInInspector] public bool isPressd; private bool _isTriggerLongPress; /// /// 是否触发了长按 /// public bool IsTriggerLongPress => _isTriggerLongPress; // 拖拽相关变量 private bool _isDragging; private Vector2 _dragStartPosition; /// /// 拖拽阈值(像素),超过这个距离才认为是拖拽 /// public float dragThreshold = 10f; /// /// 拖拽开始事件 /// public Action onDragStart; /// /// 拖拽进行中事件 /// public Action onDrag; /// /// 拖拽结束事件 /// public Action onDragEnd; public Action onPointerEnter; public Action onPointerExit; void OnPress(bool pressed) { if (pressed) // 按下 { isPressd = true; _isDragging = false; // 重置拖拽状态 } else // 松开或离开 { if (isPressd) { if (_isTriggerLongPress && !_isDragging) // 触发了长按且不是拖拽,执行 longPressEnd { longPressEnd?.Invoke(); _isTriggerLongPress = false; } } isPressd = false; _time = 0; } } private void Update() { // 如果正在拖拽,不执行长按逻辑 if (_isDragging) return; if (isPressd && !_isTriggerLongPress) { _time += Time.deltaTime; if (_time >= startTime) { _isTriggerLongPress = true; _time -= intervalTime; longPress?.Invoke(); } } else if (isPressd && _isTriggerLongPress) { _time += Time.deltaTime; if (_time >= intervalTime) { _time -= intervalTime; longPress?.Invoke(); } } } /// /// 终止 /// public void Terminated() { if (isPressd) { isPressd = false; if (_isTriggerLongPress) { longPressEnd?.Invoke(); } _isTriggerLongPress = false; _time = 0; } _isDragging = false; // 同时终止拖拽状态 } public void OnPointerDown(PointerEventData eventData) { _dragStartPosition = eventData.position; OnPress(true); } public void OnPointerUp(PointerEventData eventData) { // 如果正在拖拽,先触发拖拽结束 if (_isDragging) { OnEndDrag(eventData); } OnPress(false); } public void OnPointerExit(PointerEventData eventData) { onPointerExit?.Invoke(eventData); // 如果正在拖拽,不处理退出事件(拖拽过程中允许离开按钮区域) if (!_isDragging) { OnPress(false); } } public void OnPointerEnter(PointerEventData eventData) { onPointerEnter?.Invoke(eventData); } public void OnPointerClick(PointerEventData eventData) { // 如果发生了拖拽,不触发点击事件 if (_isDragging || _isTriggerLongPress) return; onClick?.Invoke(); isPressd = false; } // ========== 拖拽相关方法 ========== public void OnBeginDrag(PointerEventData eventData) { // 检查是否达到拖拽阈值 if (Vector2.Distance(eventData.position, _dragStartPosition) >= dragThreshold && !_isDragging) { _isDragging = true; // 开始拖拽时终止长按逻辑 if (_isTriggerLongPress) { longPressEnd?.Invoke(); _isTriggerLongPress = false; } onDragStart?.Invoke(eventData); } } public void OnDrag(PointerEventData eventData) { if (_isDragging) { onDrag?.Invoke(eventData); } else { // 如果还没有正式开始拖拽,检查是否达到阈值 if (Vector2.Distance(eventData.position, _dragStartPosition) >= dragThreshold) { OnBeginDrag(eventData); } } } public void OnEndDrag(PointerEventData eventData) { if (_isDragging) { onDragEnd?.Invoke(eventData); _isDragging = false; } } /// /// 检查是否正在拖拽 /// public bool IsDragging => _isDragging; /// /// 强制结束拖拽 /// public void ForceEndDrag() { if (_isDragging) { _isDragging = false; onDragEnd?.Invoke(null); } } }