Hi everyone, I’m attempting to solve the same problem from this post Trigger button click from code but with Unity UI instead of UI Toolkit. Our project is already mostly built with UI and working well, so overhauling to UI Toolkit is not a viable option for us.
Basically, I need a way to trigger a button click through code that also triggers the visual effects of pressing the button. I can invoke onClick pretty easily through code, but the button sprite doesn’t change like it would if I clicked it with a mouse.
I’m assuming there’s some way to do this by simulating a submit event or something through the event system, but after digging around a lot I can’t find anything like that. Does anyone know if this is possible? Or should I start down the route of remaking the visual effects from scratch and just binding that to onClick?
Don’t think about triggering the button through code. Just think about what happens after said button has been clicked. Your button calls a specific method, and you can just call the same method via code.
you can add a visual effect of pressing a button by swapping its pressed sprite like this through code. And you also need to change your button transition from color tint to sprite swap in editor. Thats how i show the effect of when a button is pressed to send the user that something has happed
but a more “complete” version I’m using in a fully-automated ui testing system:
public void RealClick(Button b)
{
var eventSystem = EventSystem.current;
if (eventSystem == null)
return;
var pev = new PointerEventData(eventSystem)
{
pointerId = -1, // simulated pointer ID (-1 for generic pointer)
position = Vector2.zero, // position is not crucial here (maybe?)
};
// Trigger the visual effects
ExecuteEvents.Execute(b.gameObject, pev, ExecuteEvents.pointerEnterHandler); // not sure if essential
ExecuteEvents.Execute(b.gameObject, pev, ExecuteEvents.pointerDownHandler);
ExecuteEvents.Execute(b.gameObject, pev, ExecuteEvents.pointerUpHandler);
ExecuteEvents.Execute(b.gameObject, pev, ExecuteEvents.pointerExitHandler); // also not sure if essential
// Trigger the logical OnClick event (I don't remember if this was needed or not, check if you're getting 2 clicks)
b.onClick.Invoke();
}
That won’t work for what I’m trying to do here. I have been able to invoke onClick through code and get the functionality part to happen. But what I need are the visual effects that are built into the Button component. Even if you don’t bind any methods to a button’s onClick event, it will still change colors/sprites as you hover and click on the button. That’s the part I’m trying to invoke through code, but that doesn’t seem to be doable it looks like. I think I’m going to have to just recreate the visuals in a custom method and use that.
well, everything happens so fast you can’t see a thing. you actually want to see the visuals with your own eyes, right? in that case, just add some delay between the calls, a coroutine is the best way of doing this (I’m not sure what’s the state of async rn in Unity).
hover, wait, pointer down, optionally wait less, pointer up, wait, unhover
Ha, I’m dumb, you’re right. I needed a time delay between the press and release and it’s working like I wanted. Thanks! ExecuteEvents.Execute() was exactly what I was looking for, I’ll have to look more into this in general.