Files
ichni_Official/Assets/Scripts/UI/Base/ModifyValueButton.cs

76 lines
2.0 KiB
C#
Raw Normal View History

2025-08-11 14:04:06 -04:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Ichni.UI
{
public class ModifyValueButton : MonoBehaviour, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler
{
public float holdThreshold = 0.5f;
private bool isPointerDown = false;
private bool isLongPress = false;
private float pointerDownTimer = 0f;
private float longPressTriggerInterval = 0.1f;
private float longPressTimer = 0f;
public TextButton button;
public UnityAction tapAction;
public UnityAction longPressAction;
public void SetUpButton(UnityAction tapAction, UnityAction longPressAction)
{
button ??= GetComponent<TextButton>();
this.tapAction = tapAction;
this.longPressAction = longPressAction;
}
void Update()
{
if (isPointerDown)
{
pointerDownTimer += Time.deltaTime;
if (pointerDownTimer >= holdThreshold && !isLongPress)
{
isLongPress = true;
}
}
if (isLongPress)
{
longPressTimer += Time.deltaTime;
if (longPressTimer >= longPressTriggerInterval)
{
longPressTimer = 0f;
longPressAction?.Invoke();
}
}
}
public void OnPointerClick(PointerEventData eventData)
{
if (!isLongPress)
{
tapAction?.Invoke();
}
}
public void OnPointerDown(PointerEventData eventData)
{
isPointerDown = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isPointerDown = false;
isLongPress = false;
pointerDownTimer = 0f;
}
}
}