- Home /
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?
Try EventSystem.SetSelectedGameObject, or possibly calling OnDeselect manually.
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.
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.
Answer by IsaiahKelly · Feb 19, 2016 at 02:33 AM
I've discovered two ways to do this:
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.
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);
Thanks for reply. I won't be able to get to it until next week, but I'll give it a try.
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.
@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!
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)
Works really nice! No double selection if hover done and then input changee to joystick/keyboard.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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