- Home /
Interact with uGUI button using code?
Hello,
I'm having trouble trying to interact with uGUI buttons using code. My goal is to simulate the usual behaviour of the standard button, changing from "normal color", to "highlighted color", to "pressed color" but using code instead of touch or a mouse.
Right now my standard button I have a method that detects when a "cursor" GameObject (Not attached to the mouse or touch position) is inside the boundaries of the button. I'd like that when it's detected that the gameobject is inside the boundaries the animation from "normal" to "highlighted" plays as if the mouse cursor was over the button.
Similarly I want to invoke the "onClick" event as if I clicked or touched the button, but using code.
I was expecting some method like button.isHighlighted(true) and button.isClicked(true) so I could manually change the appearence and ultimately interact with the button.
Is there any way I can achieve this?
If it helps, picture the following situation: An AI cursor in a tutorial shows up and "hovers" above the UI button and then "press" it launching the usual OnClick behavior defined in the inspector.
Thanks in advance, Sergio
Answer by DoTA_KAMIKADzE · Apr 23, 2015 at 01:04 PM
You're looking for OnSomething public functions of button (e.g. OnPointerDown, OnPointerEnter, etc.). HERE is one of my answers that will show you how to implement them in code. Attach that code to your button as component and do what you expect on specific event.
P.S. Check this answer as well, just to keep that in mind.
P.P.S. An update to answer your updated question:
So you want to simulate inputs on your button. In order to do that you can use EventSystems.ExecuteEvents.Execute. I'll show you an example how to use that:
ExecuteEvents.Execute(yourButton.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerEnterHandler); // that will trigger OnPointerEnter event.
Thanks for you answer DoTA_$$anonymous$$A$$anonymous$$I$$anonymous$$ADzE but that's not what I'm looking for.
I already know how to react to different events such as OnPointerDown, OnClick, etc. But I want to do is to interact with the button using code, in a similar manner as if using the mouse cursor.
IE: - Calling a function "buttonHighlight" the button becomes highlighted as if the cursor is over it. - Calling a function "buttonClick" the button will play the "pressed" animation in the same way as if the mouse clicked it.
So far i've managed to use Button.Select() to highlight the button as I want, but I can't find a "Button.Unselect()" (although selecting other button works) and I can't find a "Button.Click()" function either.
Check out my updated answer.
P.S. Events from your comment that you should use in my P.P.S. section of answer:
deselectHandler - to "unselect"
pointerClickHandler or pointerDownHandler - to "click"
Thanks, this puts me in the right track :D
$$anonymous$$arked as answer, thanks a lot.
It's almost working but I have a problem with deselectHandler.
I have a button with 3 colors (Green = normal, Yellow = highlighted, Red = pressed) and a function that returns if the AI object is over the button and if i'ts pressing, which returns an enum. Then I evaluate the value like this:
switch (status) {
case statusType.normal:
if (pressed)
{
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerUpHandler); // Not pressing the button
pressed = false;
}
if (highlighted)
{
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.deselectHandler); // The cursor is not even near
highlighted = false;
}
break;
case statusType.highlighted:
if (!highlighted)
{
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerEnterHandler); // Highlight the button
highlighted = true;
}
if (pressed)
{
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerUpHandler); // The button isn't pressed anymore
pressed = false;
}
break;
case statusType.pressed:
if (!pressed)
{
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerDownHandler); // Pressed animation
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler); // Launch click event
pressed = true;
}
break;
}
I use boolean variables pressed and highligted to not call Execute in each update.
So far it works O$$anonymous$$. The gameobject enters the button area and the button is highlighted. If the gameObject "presses" the button is pressed with it's animation and launch the method defined in the OnClick section of the button in the inspector.
BUT for some reason when the gameObject leaves the button it reverts to the highlighted status, even though in code it says it's not highlighted.
I've tried with ExecuteEvents.deselectHandler, ExecuteEvents.cancelHandler... to no avail
I didn't understand how exactly you interact, but anyway:
Highlighted =/= selected (even though they use same visual effect by default).
Highlighted is basically pointerEnterHandler/pointerExitHandler, while select is selectHandler/deselectHandler.
P.S. If that^ won't help you then I think I'd need some example code of how your things are done.
Your answer
Follow this Question
Related Questions
Button Action Triggering Multiple Times Per Click? 1 Answer
Issue with spawning buttons and assigning listeners 1 Answer
Is there a way to move a button to a random location after a certain number of clicks? 1 Answer
Unity multiple button on click 1 Answer
Execute by code a pattern of buttons 0 Answers