Unity Prevent Click On Button

3k Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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:

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.UI;

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;

    public bool WasLongPressTriggered
    {
        get;
        protected set;
    }

    private void Update()
    {
        if (isPointerDown && false == longPressTriggered)
        {
            if (Time.time - timePressStarted > durationThreshold)
            {
                longPressTriggered = true;
                WasLongPressTriggered = true;
                onLongPress.Invoke();
            }
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        timePressStarted = Time.time;
        isPointerDown = true;
        longPressTriggered = false;
        WasLongPressTriggered = false;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        isPointerDown = false;
    }


    public void OnPointerExit(PointerEventData eventData)
    {
        isPointerDown = false;
    }
}

Then you will need a new button script to override the old basic button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Button2 : Button
{
    public bool PreventPropagationOnLongPress;

    public override void OnPointerClick(PointerEventData eventData)
    {
        var longPress = GetComponent<LongPressButton>();
        if (longPress == null)
        {
            base.OnPointerClick(eventData);
        }
        if(false == longPress.WasLongPressTriggered)
        {
            base.OnPointerClick(eventData);
        }
    }
}

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,