using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; [RequireComponent(typeof(Button))] public class ExtendButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { [SerializeField] Button button; [SerializeField] float pressedInternalTime = 0.5f; [SerializeField] float pressedTickIntervalTime = 0.1f; bool isPressed; float pressedTime; float prevCallPressedEventTime; public Button ButtonComponent => button; public event Action OnPressedShortly; public event Action OnLongPressed; public event Action OnLongPressedReleased; private void OnEnable() { prevCallPressedEventTime = 0; pressedTime = 0; isPressed = false; } private void OnDisable() { prevCallPressedEventTime = 0; pressedTime = 0; isPressed = false; } private void LateUpdate() { if (!isPressed) return; if (Time.time >= prevCallPressedEventTime) { OnLongPressed?.Invoke(); prevCallPressedEventTime += pressedTickIntervalTime; } } public void OnPointerDown(PointerEventData eventData) { if(!button.interactable) return; isPressed = true; pressedTime = Time.time; prevCallPressedEventTime = pressedTime + pressedInternalTime; } public void OnPointerUp(PointerEventData eventData) { if (pressedTime + pressedInternalTime <= Time.time) OnLongPressedReleased?.Invoke(); else OnPressedShortly?.Invoke(); prevCallPressedEventTime = 0; pressedTime = 0; isPressed = false; } private void OnValidate() { if (button == null) button = GetComponent