I'm creating a script for a long button press. The long button press works, The only issue is that if the long button press triggers I want to prevent the click on the button.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
public class LongPressButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
[Tooltip("How long must pointer be down on this object to trigger a long press")]
public float durationThreshold = 1.0f;
public UnityEvent onLongPress = new UnityEvent();
private bool isPointerDown = false;
private bool longPressTriggered = false;
private float timePressStarted;
private void Update()
{
if (isPointerDown && !longPressTriggered)
{
if (Time.time - timePressStarted > durationThreshold)
{
longPressTriggered = true;
onLongPress.Invoke();
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
timePressStarted = Time.time;
isPointerDown = true;
longPressTriggered = false;
}
public void OnPointerUp(PointerEventData eventData)
{
isPointerDown = false;
}
public void OnPointerExit(PointerEventData eventData)
{
isPointerDown = false;
}
}
The button script is black boxed, it seems like its build into the unity engine.
I know there are ways to prevent event propagation to the 3d world but is there a way to prevent propagation on a button script?