Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by PsyDev · Nov 17, 2015 at 03:58 PM · uieventsystemhighlightselectable

How do I de-highlight a UI Selectable after disabling mouse input?

I have a UI element in the center of the screen. If you hover over it with the mouse, it becomes highlighted. Now disable mouse input with Cursor.lockState = CursorLockMode.Locked and hide the mouse with Cursor.visible = false. However, the UI element remains in a highlighted state. How can I trigger the event system to transition this from Highlighted to Normal state?

Comment
Add comment · Show 4
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 Cherno · Nov 17, 2015 at 10:21 PM 0
Share

Try EventSystem.SetSelectedGameObject, or possibly calling OnDeselect manually.

avatar image PsyDev Cherno · Nov 17, 2015 at 11:09 PM 0
Share

Thanks for the reply. EventSystem.SetSelectedGameObject does not unhighlight any Selectables that the mouse is hovering over. That is the problem. Even without any of the Cursor stuff I mentioned above, if you hover over a Selectable, then in code set another object as selected with EventSystem.SetSelectedGameObject, the hovered object stays highlighted.

OnDeselect is a callback that you can override and gets called when an object does get deselected.

avatar image Cherno PsyDev · Nov 17, 2015 at 11:26 PM 0
Share

If it's a button, maybe you can disable the button? The image component that belongs to the button gameobejct would still be active, of course.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by IsaiahKelly · Feb 19, 2016 at 02:33 AM

I've discovered two ways to do this:

  1. Call ClearSelection on the active input module to deselect/unhighlight all UI elements. However, this is a protected class, so it's only accessible from within custom input modules that derive from the PointerInputModule.

  2. Use the ExecuteEvents helper class to send a pointer exit event to a specific game object to unhighlighted that object.

Example:

 var pointer = new PointerEventData(EventSystem.current);
 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, pointer, ExecuteEvents.pointerExitHandler);

@PsyDev

Comment
Add comment · Show 5 · 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 PsyDev · Feb 19, 2016 at 02:20 PM 0
Share

Thanks for reply. I won't be able to get to it until next week, but I'll give it a try.

avatar image IsaiahKelly PsyDev · Feb 19, 2016 at 02:40 PM 0
Share

Yeah, let me know how it goes because I'm not sure if locking the cursor will actually disable the pointer input for the UI. If it doesn't, you'll probably need to make a custom input module that disables the pointer before using one of the methods above.

avatar image lassade · Nov 09, 2016 at 03:08 AM 2
Share

@Isaiah$$anonymous$$elly only the FRIST method worked with 5.4.1f1. This is my final code:

 public class CustomInput$$anonymous$$odule : StandaloneInput$$anonymous$$odule
 {
     bool cursorVisible = true;
 
     //inherited event processing interface (called every frame)
     public override void Process()
     {
         bool usedEvent = SendUpdateEventToSelectedObject();
 
         if (eventSystem.sendNavigationEvents)
         {
             if (!usedEvent)
                 usedEvent |= Send$$anonymous$$oveEventToSelectedObject();
 
             if (!usedEvent)
                 SendSubmitEventToSelectedObject();
         }
 
         if (cursorVisible != Cursor.visible)
         {
             if (!Cursor.visible)
             {
                 var selected = eventSystem.currentSelectedGameObject;
                 ClearSelection();
                 eventSystem.SetSelectedGameObject(selected);
             }
             cursorVisible = Cursor.visible;
         }
 
         if (Cursor.visible)
         {
             Process$$anonymous$$ouseEvent();
         }
     }
 }

This code is so damn obscure it took me hours to pice all together. Thanks by the way!

avatar image rboerdijk lassade · Mar 10, 2020 at 10:21 PM 1
Share

wow.. sorry for necro, but this code-snippet is brilliant. I spend hours trying to get keyboard-navigation and mouse-navigation working and was already messing around with my own custom inputmodule (arrived at the point trying to figure out how to deselect the highlighted control by the mouse on hiding it) when I stumbled on this post.

With the above code I finally got to have a solution for having both keyboard navigation (hiding the mouse) and navigating between controls with the keys, and mouse-navigation unhiding the mouse and taking over from keyboard navigation - without those two systems (selected vs highlight) breaking each other in every possible way ( 1 button selected, another highlighted and variations thereof). When the keyboard selects something and you continue with the mouse, it now seamlessly takes over with the selection, and vice-verse!

(For the record, using Unity 2019.3.3f1)

avatar image maltakereuz lassade · Sep 06, 2020 at 03:21 PM 0
Share

Works really nice! No double selection if hover done and then input changee to joystick/keyboard.

avatar image
0

Answer by airespt · Jan 26, 2017 at 05:15 PM

Hi, calling OnDeselect() manually worked for me on 5.5.0f3. Tested on a simple button like this:

 yourButton.OnDeselect(null);

and the 'yourButton' returned to normal state.

Comment
Add comment · 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

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

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Selecting a selectable using script doesn't seem to work anymore? 1 Answer

keyboard submit not working on selectables. 1 Answer

Is the 'highlighted state' the same as the 'selected state'? 2 Answers

NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject() 1 Answer

EventSystem.SetSelectedGameObject Does not display highlight animations for my buttons 1 Answer


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