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?
I've figured it out, Due to Unity limitations of there event system you will need two scripts to do so. First the long button press script:
Then you will need a new button script to override the old basic button:
This works in the editor it has yet to be fully tested on mobile, potentially in the unity edidor the on point click is called but on mobile it could be triggered from on mouse up... for the time being this will prevent the event propagation,