Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SVC Games · Apr 23, 2015 at 01:01 PM · uibuttonbutton trigger events

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SVC Games · Apr 23, 2015 at 02:41 PM 0
Share

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.

avatar image DoTA_KAMIKADzE · Apr 23, 2015 at 02:50 PM 1
Share

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"

avatar image SVC Games · Apr 23, 2015 at 03:15 PM 0
Share

Thanks, this puts me in the right track :D

$$anonymous$$arked as answer, thanks a lot.

avatar image SVC Games · Apr 23, 2015 at 04:09 PM 0
Share

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

avatar image DoTA_KAMIKADzE · Apr 23, 2015 at 04:17 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges