using System; using Framework; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class ButtonUpDrag: MonoBehaviour, IPointerDownHandler, IPointerUpHandler { private bool _isPressed; private bool _isLastFramePressed; private float _pressedTime; private const float MinDelta = 0.01f; private const float MinLongPressTime = 0.35f; public static float MinUpDelta = 50f * (Screen.height / 1080f); private float _maxHorDelta = 0f; private bool _bUpDrag; private bool _bLongPress; private bool _bOnClick; public delegate void OnUpDrag(); public delegate void OnLongPress(); public delegate void OnClick(); public delegate void OnPointUp(); public delegate void OnPointDown(); public OnPointUp onPointUp; public OnPointDown onPointDown; public OnUpDrag onUpDrag; public OnLongPress onLongPress; public OnClick onClick; private Vector2 _startPos; private Vector2 _curPos; public void AddPointerUp(Action action) { onPointUp += () => { action?.Invoke(); }; } public void AddPointerDown(Action action) { onPointDown += () => { action?.Invoke(); }; } public void AddLongPress(OnLongPress action) { onLongPress += action; } public void AddLongPress(Action action, T param) { onLongPress += () => { action?.Invoke(param); }; } public void AddUpDrag(Action action) { onUpDrag += () => { action?.Invoke(); }; } public void AddUpDrag(Action action, T param) { onUpDrag += () => { action?.Invoke(param); }; } public void OnPointerDown(PointerEventData eventData) { _isPressed = true; _bLongPress = true; _bUpDrag = true; _startPos = eventData.pressPosition; } public void OnPointerUp(PointerEventData eventData) { // _isPressed = false; // _bLongPress = true; // _bUpDrag = true; } private void Awake() { InputManager.Instance.OnFingerUpUI += _OnPutUp; InputManager.Instance.OnFingerUp += _OnPutUp; } private void OnDestroy() { if (InputManager.Instance) { InputManager.Instance.OnFingerUpUI -= _OnPutUp; InputManager.Instance.OnFingerUp -= _OnPutUp; } } private void Update() { if (!_isPressed) { _bOnClick = false; _isLastFramePressed = false; _pressedTime = 0f; _maxHorDelta = 0f; return; } if (_isPressed) { _curPos = InputManager.Instance.GetTouchPosition(); //长按 if (_bLongPress) { var disSqr = (_curPos - _startPos).sqrMagnitude; if (disSqr > MinDelta) { _bLongPress = false; _bOnClick = false; } else { if (_pressedTime > MinLongPressTime) { onLongPress?.Invoke(); _bLongPress = false; } } } //上滑 if (!_bLongPress && _bUpDrag) { var disSqrY = Mathf.Sqrt(Mathf.Pow(_curPos.y - _startPos.y, 2)); var disSqrX = Mathf.Sqrt(Mathf.Pow(_curPos.x - _startPos.x, 2)); //防止拖过去拖回来少算 if (_maxHorDelta < disSqrX) { _maxHorDelta = disSqrX; } //调节水平拖动敏感程度 if (_maxHorDelta < MinUpDelta * 4) { if (disSqrY > MinUpDelta) { onUpDrag?.Invoke(); _bUpDrag = false; } } } _pressedTime += Time.deltaTime; if (!_isLastFramePressed) { _isLastFramePressed = true; } } } private void _OnPutUp(Vector2 pos) { if (_isPressed) { _isPressed = false; _bLongPress = true; _bUpDrag = true; } } public void RemoveAll() { onLongPress = null; onPointDown = null; onPointUp = null; onClick = null; onUpDrag = null; } }