IIRC “Deselect On Background Click” doesn’t work correctly if you click on a disabled control. The input module tries to select the disabled control and realises it can’t, so sets the current selection to null. The background check ONLY checks for cases where the mouse click isn’t over a control, so it’s not a suitable fix.
I’m grateful to Unity for adding this feature after I requested it, but it shouldn’t have been necessary in the first place, the wait for implementation was too long, and the resulting feature was inadequate. I worked around the issue by building my own UI system on top of canvases and the UI interfaces (e.g. IPointerEnterHandler) instead.
I figured Selectable was the root of the problem, so made an alternative out of small independent modules that let me mix and match functionality such as selectable, clickable, navigable, etc. It works incredibly well, allowing smooth switching between input devices (gamepad, mouse, touch, etc) and finer control over things. No more workarounds for issues like this, and adds a few missing features such as being able to show keyboard/gamepad focus and mouse hover state separately (something that always bothered me about uGui).
Was it a lot of work? Yes. Was it worth it? Absolutely. We shipped one incredibly successful title with it on PC, Mac and Switch where it suports mouse, keyboard, gamepad and touch. It also works great on mobile and tablets, although we haven’t shipped those. I built it as a package, so it’s completely reusable and we’re already using it in our next game.
I say this not to blow my own trumpet, but to highlight that Unity really needs to eat its own dog food. We all know it but cancelling Gigaya was a mistake.
6 Likes
Deselect on background also doesn’t prevent the user to deselect the UI by right-clicking, which is something I would love to prevent.
1 Like
I’m trying to deal with mouse vs controller woes by implementing a custom Selectable that only shows/updates the selection when using controller/keyboard, and only shows the highlight when using mouse - so if using mouse, there’s still a valid keyboard/controller selection, just hidden.
It mostly works, but the aggressive/unnecessary deselection is getting in the way of this, and it looks like the only real workaround is editing the UI package itself 
Unbelievable this is still a thing almost 5 years later. If any of you are interested in fixing the deselection upon clicking disabled controls (interactable set to false) and upon clicking a control with any of the other mouse buttons, I modified InputSystemUIInputModule.cs with the following at around Line 471 and it seems to do the trick:
var selectHandler = ExecuteEvents.GetEventHandler<ISelectHandler>(currentOverGo);
var selectable = currentOverGo != null ? currentOverGo.GetComponent<Selectable>() : null;
var isInteractable = selectable != null ? selectable.interactable : false;
// If we have clicked something new, deselect the old thing and leave 'selection handling' up
// to the press event (except if there's none and we're told to not deselect in that case).
if (selectHandler != eventSystem.currentSelectedGameObject && (selectHandler != null || m_DeselectOnBackgroundClick)) {
if (m_DeselectOnBackgroundClick)
eventSystem.SetSelectedGameObject(null, eventData);
else if (selectHandler != null && isInteractable && eventData.button == PointerEventData.InputButton.Left) {
eventSystem.SetSelectedGameObject(null, eventData);
}
}
You’ll have to embed the input system package into your project to do this, which you’ll likely have to do anyways like I had to for 1.7.0 to apply a bug fix manually since they don’t want to backport it to a STABLE release for some reason…I’ll stop ranting.
3 Likes
As a workaround, it seems like also possible to use CanvasGroup (if you need to block multiple buttons in another panel) with Interactable = false and BlockRaycast=false
This is making me very sad.
Just trying to support touchscreen and gamepad without things breaking. Some menu items are navigable and some and bound to EG shoulders with an on-screen (tappable) element. Currently if you touch a non-navigable button onscreen and even if you drag out of it before releasing so it doesn’t trigger it still deselects everything on screen breaking the gamepad navigation until you tap back on a navigable element.
This feels very sub par.
Please give us an option to ignore non-navigable elements or just disable deselection all together so we don’t end up with no selection and broken navigation.
Thanks for sharing, this saved me a lot of debugging time.
I think it’s a shame that Unity doesn’t offer quicker ways to implement/customize some standard UI features that are present in most games these days. They should prioritize UI systems more, as we often wait years for these fixes to be available to everyone.