In GUI, there’s something called GUI.ButtonRepeat which fires every frame as long as the button is pressed. I was expecting - hoping - that the new GUI button would have event similar to Button Repeat, or that there would a similar class.
However, I simply can’t find it.
On top, extending the existing GUI class appear to be a small puzzle on itself as the existing class have quite the extensive custom editor - and I deeply hate writing custom editor.
So, how should go using the new GUI to make a button that fires every frame it is held down?
Add an Event Trigger Component to the Button object and click on “Add New”. There you have every event you can use on that button, including PointerDown and PointerUp.
EDIT: Huh… OnPointerDown is fired only once, the moment the mouse button is pressed. Ok, so how do I make this event repeat itself as long as it’s held down?
Set it up so a bool is set to true on the OnPointerDown event and set to false on the OnPointerUp event.
In your Update method check that boolean. If its true, the button is held down.
For anyone else that stumbles onto this like I did, I made the following script to streamline the repeat button functionality per the previously suggested solution until Unity comes up with something better. Did the trick for me. Probably add a cooldown or something (as needed) unless you really want it firing every frame.
Just throw this MonoBehaviour on the same object as your button, assign the button property, and you’re in business.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
[RequireComponent(typeof(Button))]
public class ButtonRepeater : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public bool shouldRepeat { get { return _pointerData != null; } }
public Button button = null;
private PointerEventData _pointerData = null;
I could not make work your solution. I have a weird "Unexpected symbol ‘’ " in the console that prevent me from compiling the code. Also I was a bit confused with the bool shouldRepeat. Why to not call it : “pointerIsDown”.
since earlier code had hidden symbols that broke compile here is one that works…
button should be assigned automatically, while I introduced a delay for the repeat to start and interval for it to work.
might have a bug, but seems to work
still works, one gotcha to watch out inside editor:
For the first click, it fires OnPointerUp() right after OnPointerDown(), even if you are holding mouse down!
So click game view first to activate it(?), then click and hold the button to see real results…