- Home /
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.
Answer by LeonmFF · Aug 21, 2018 at 07:48 PM
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.
haha..yeah it resembles my personality. But this method is also shooting only one time. Thank You for helping though. Even I tried PointerClick, PointerEnter, etc... but they all are working same, one time shoot only.
Answer by Ahsanhabibrafy · Jul 18, 2020 at 09:15 AM
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;
}
}
Answer by HardyForce · Jan 24, 2020 at 06:37 PM
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
Answer by Vicarian · Aug 21, 2018 at 07:46 PM
I'd use the inspector and add an EventTrigger component, then implement PointerDown. If you're unsure how, visit https://unity3d.com/learn .
Answer by venomjadhav · Jul 03, 2020 at 06:12 AM
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.
Your answer
Follow this Question
Related Questions
Highlighted buttons stay highlighted when I deactivate a canvas. No way to unhighlight them. 1 Answer
How to Change Menu Button Rollover States with GameObjects 1 Answer
UI button mobile - want a button drag to register as a click 0 Answers
How do I highlight multiple buttons at once? 1 Answer
Tapping a button and screen interference 0 Answers