I recommend that you check the ExecuteEvent class (I had a question similar to yours here). If you want to simulate a mouse hover on any object with a RectTransform, in this case named “button”, you can do this:
var pointer = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.pointerEnterHandler);
This will trigger the enter (hover) event, which listeners might be waiting for. For Buttons, you will see that it also triggers the visual hovered/highlighted state. Likewise, you can use other handlers defined in ExecuteEvents, such as pointerDownHandler, pointerUpHandler or submitHandler, triggering other events (down, up, click, etc).
Just know that, for a Button, Unity only visually changes it to its “down” or “up” state if it is hovered first, so using pointerEnterHandler is required beforehand. However, all events still trigger as normal, meaning that if you execute pointerDownHandler without pointerEnterHandler first, the OnPointerDown trigger will still be fired by the button object; only the Button will not change visually.
A test example, for the sake of completeness:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ExecuteEventsOnButton : MonoBehaviour
{
public Button button;
void Update()
{
var pointer = new PointerEventData(EventSystem.current); // pointer event for Execute
if (Input.GetKeyDown(KeyCode.H)) // force hover
{
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.pointerEnterHandler);
}
if (Input.GetKeyDown(KeyCode.U)) // un-hover (end hovering)
{
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.pointerExitHandler);
}
if (Input.GetKeyDown(KeyCode.S)) // submit (~click)
{
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.submitHandler);
}
if (Input.GetKeyDown(KeyCode.P)) // down: press
{
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.pointerDownHandler);
}
if (Input.GetKeyUp(KeyCode.P)) // up: release
{
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.pointerUpHandler);
}
}
}
You can see what happens if button
is of class Button and you try to press P before pressing H (nothing happens). Press H to hover the button and then P to press it (keep P down for as long as you want, before releasing).
From this example and the documentation page, you should be able to do exactly what you want.
There is also a pointerClickHandler (I’m referring this as you also asked for a simulated “click” event), but I believe it doesn’t trigger the visual changes in the Button by itself (even when using pointerEnterHandler first), so you might need to mix it with pointerDownHandler/pointerUpHandler, or simply use submitHandler (which works as if you pressed the Enter key with the button selected).
Finally, in case you or someone has tried, don’t bother calling methods OnPointerEnter, OnPointerDown, etc on the Button (actually, Selectable) class, because these will affect the button visually but will do nothing else, as no events are triggered.