How can I perform hold event to Button via script.
Something like that :
_holdButton.HoldDown();
Sleep(2);
_holdButton.HoldUp();
To be clear, do you mean:
simulate that the button is pressed down, wait 2 seconds like that, then simulate it was released?
By simulate, I mean visually… and after it’s up, does it call some function(s) ?
I want to simulate by source code (c# lanaguage). For simulate click we can use ButtonClickedEvent, but for hold down/up, I don’t know how can I do it.
Not sure if ButtonClickedEvent simulates but that is beside the point.
If you just want it to change colour or something and look like it’s down for 2 seconds, you could do that… Just use its Pressed Colour (change to that), wait 2 seconds, change back to normal (+ fire method(s)) ?
if you want to simulate button holding you’ll need to manually call the OnPointerDown, OnPointerUp and OnClick (in that order) methods on the button. You’ll need to create and send your own PointerEventData instance but it shouldn’t be too hard.
I had found solution to implement as above
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class SimButtonHold : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler {
[SerializeField]
Button _Button;
void Start() {
PointerEventData pointer = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(_Button.gameObject, pointer, ExecuteEvents.pointerDownHandler);
Invoke(“DelayedRelease”, 3.0f);
}
private void DelayedRelease() {
PointerEventData pointer = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(_Button.gameObject, pointer, ExecuteEvents.pointerUpHandler);
}
public void OnPointerClick(PointerEventData eventData) {
Debug.Log(“OnPointerClick”);
}
public void OnPointerDown(PointerEventData eventData) {
Debug.Log(“OnPointerDown”);
}
public void OnPointerUp(PointerEventData eventData) {
Debug.Log(“OnPointerUp”);
}
}
Cool. Glad ya got it working.