- Home /
How to make UI Button Fire Continuously ?
how can I make UI Button Fire Continuously when clicked and hold ?
Answer by davidcox70 · Apr 09, 2018 at 10:32 PM
Have a look at InvokeRepeating and Cancel Invoke.
Where you would normally handle the button press, use something like;
InvokeRepeating("FireMe",0, 0.1f);
This will continuously trigger a function called "FireMe" every one tenth of a second. Of course, at some point you will want to stop firing! So test when the user has stopped pressing the fire button (maybe OnMouseUp or Input.GetMouseButton(0)==false if the mouse is the fire button), and execute;
CancelInvoke("FireMe");
Hope that helps.
DC
edit. Detecting a UI button press isn't as simple as detecting a "click" (which is a press down followed by a release.). So just in case, this is how you detect it with a script attached to the button itself;
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;
public class myAutoFire : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public void OnPointerDown(PointerEventData eventdata){
InvokeRepeating ("FireMe", 0, 0.1f);
}
public void OnPointerUp(PointerEventData eventdata){
CancelInvoke ("FireMe");
}
void FireMe(){
Debug.Log ("firing"+Time.time);
}
}
and which function should i select in OnClick section of my button in inspector ?
my script is located on other gameobject
Answer by suleymanbucuk · Apr 30, 2020 at 08:33 PM
There is my solution for the situation; When you press a UI Button continuously, shooting or another function is called repeatedly:
Create a button in UI menu, remove or disable Button script from that button. Create a new sscript (my script's name is LongButtonPress). And implement the code below for shoot repeatedly when you press and hold the button.
using UnityEngine; using UnityEngine.EventSystems;
public class LongButtonPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { public bool isPressed = false;
public void OnPointerDown(PointerEventData eventData)
{
isPressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isPressed = false;
}
private void Update()
{
if(isPressed)
{
// implement the code here whatever you want to be when you press the button.
}
}
}
This code sets isPressed boolean true when you press the button, and in Update method if isPressed is true, applies the code in if statement.
Works pretty well for my game. Be care when you copy the code, some lines are not shown in code block in my comment, such as use UnityEngine etc. They are upperside of code block shown by forum.
Answer by LicketyCut · Jan 15, 2021 at 01:13 AM
Here is a Class that I wrote that does just what you are looking for.
Attach it to the Button GameObject and assign the same OnClick event to the ButtonRepeater class that you attach to the Button Class.
If you don't need a delay between clicks then leave delay at 0.
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using static UnityEngine.UI.Button;
public class ButtonRepeater : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public float delay;
public ButtonClickedEvent onClick;
bool pressed;
public void OnPointerDown(PointerEventData eventdata)
{
pressed = true;
StartCoroutine(Repeat());
}
public void OnPointerUp(PointerEventData eventdata)
{
StopAllCoroutines();
pressed = false;
}
IEnumerator Repeat()
{
yield return new WaitForSeconds(delay);
while (pressed)
{
onClick?.Invoke();
yield return new WaitForSeconds(delay);
}
}
}
Thanks. Glad I could help. If you would up vote my answer it will gain visibility to help others too.
Your answer
Follow this Question
Related Questions
How to draw GUI texture under UI elements? 0 Answers
UI Button multiple parameters 6 Answers
Move gameobject to button in new 4.6? 0 Answers
Buttons not Clickable inside of ScrollView 0 Answers
Why isn't curved UI asset working? 0 Answers