- Home /
Unity UI/Events - how to do something for as long as UI button is pressed
I need to figure it for my level editor which basically has control like this:
and I have trouble coding off the "arrows" which increase value as long as mouse button is held over them. I've tried PointerDown, but that only worked once, not continuously as I need it to do. Any help?
You could start a new coroutine that begins incrementing on PointerDown, and then wait for PointerUp before stopping it?
Could work, yes. But this seems to me like a "hacky" solution and I'd rather not unless there is no other way.
Answer by JosueMariscal · May 19, 2015 at 08:02 PM
you could use Input.GetMouseButton(0) when the button is clicked.
http://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html
Answer by Kiwasi · May 19, 2015 at 10:16 PM
You'll need a custom component for this. Implement the IPointerDownHandler and IPointerUpHandler interfaces to receive the EventSystem callbacks for these events.
You may also be interested in the IPointerEnterHandler, otherwise you will not receive a callback if the user moves the pointer away from the control before releasing the mouse button.
Anyway the full list of supported event handlers is here. Just pick and choose the ones that are required to make your control work.