How to make the button respond to touch and hold feature?

Hi, I have some UI Buttons in my game. Now they work perfectly when clicked one time. But I want that they should work continuously if the Button is pressed and held for some time. Like, for the Fire Button. Where is change needed, in the script or in the Inspector?? Please help! Thanks!!

Edit - I have tried the two previous answers, but they all doesn’t made any difference. thank you for them though.

Hi o/
I’ll help you because you’re a cool budy.

Go to your button in the Hierarchy, and in the inspector go to “Add Component” → Event Trigger → “Add New Event Type” and there you go!

If I’m not mistaken, the event “PointerDown” is the one you want, it’s like a “hold event”.

The rest is just like OnClick() that you already know your way around it.

Hi there @coolbudy1998!

I was also looking for a solution for this matter, and I created mine.

I know it’s quite some time since the question, but here is my take.

You only need to attach this script (HoldClickableButton.cs) to your button and set its events “OnClicked” and “OnHoldClicked”.
Also, set the “Hold Duration” value in the button’s gameobject.

Bellow, there is a sample of implementation you can do in your manager to set the events.

[SerializeField] private HoldClickableButton _button;

private void Awake()
{
    SetEvents();
}

private void SetEvents()
{
    _button.OnClicked += OnClicked;
    _button.OnHoldClicked += OnHoldClicked;
}

private void OnClicked()
{
    // TODO: Implement button's click behaviour
}

private void OnHoldClicked()
{
    // TODO: Implement button's holdclick behaviour
}

I hope it can help somebody as well.

=]

As I found no solution on the internet I finally came with one. To continuously shoot on Ui Button hold use this script.Add this script in ur Ui ShootButton.`

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class ShootHandler : MonoBehaviour,IUpdateSelectedHandler,IPointerDownHandler,IPointerUpHandler
    {
        public bool isPressed;
    
        // Start is called before the first frame update
        public void OnUpdateSelected(BaseEventData data)
        {
            if (isPressed)
            {
             Shoot();
            }
        }
        public void OnPointerDown(PointerEventData data)
        {
            isPressed = true;
        }
        public void OnPointerUp(PointerEventData data)
        {
            isPressed = false;
        }
    }

There are so many ways to achieve that.

as @Ahsanhabibrafy sad:

public void OnUpdateSelected(BaseEventData data)
         {
             if (isPressed)
             {
              Shoot();
             }
         }
         public void OnPointerDown(PointerEventData data)
         {
             isPressed = true;
         }
         public void OnPointerUp(PointerEventData data)
         {
             isPressed = false;
         }
     }

Using OnMouseDown(), OnMouseDrag(), etc…


Also you can use Raycasting and create a script using Input.GetKey() to dragg. GetKeyDown(), GetKeyUp()…

Set a bool to true on PointerDown, and false on PointerUp. Then in your Update function, check if the bool is true before your code executes

I’d use the inspector and add an EventTrigger component, then implement PointerDown. If you’re unsure how, visit Learn Game Development Without Coding Experience | Unity .

I think I have a solution. Even my character behaved the same when I tried to move it with buttons, with the PointerDown Event added. I added another one called UpdateSelected. To do that, click “Add New Event Type”, and click UpdateSelected. Do your thing by adding the necessary gameobject and panel and you’re good to go. Hope it helps.