Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Feature Request Make "deselectOnBackgroundClick" configurable without a custom InputSystemUIInputModule component

Discussion in 'Input System' started by beevik_, May 1, 2024.

  1. beevik_

    beevik_

    Joined:
    Sep 27, 2020
    Posts:
    108
    The default behavior of the InputSystem when used with UI Toolkit is to cause deselection of the focused visual element when mouse-clicking anywhere in the "background" area. (I don't understand why "deselect on background click" is the default behavior. It's almost never something you want.)

    As far as I know, the only way to prevent this behavior is to use a custom EventSystem with a properly configured InputSystemUIInputModule component, followed by a call to SetUITookitEventSystemOverride before the custom event system receives its Start callback . This is true even with InputSystem version 1.8, whose documentation suggests you shouldn't need to use SetUITookitEventSystemOverride anymore.

    So here is my feature request. Can we get some sort of project-wide InputSystem setting that disables "deselect on background click"? In fact, can we get the ability to configure the other settings only exposed through the InputSystemUIInputModule module (e.g., "cursor lock behavior")?

    In case anyone finds this post and wants to know how to prevent deselect on background click as things stand today, here are the steps:

    1. Add EventSystem and InputSystemUIInputModule components onto a GameObject in your scene.
    2. Configure the input system UI input module component by turning off "deselect on background click".
    3. Configure the input system UI input module component by settings its input "Actions Asset" to the project-wide input actions asset.
    4. Create a custom C# script component on the same GameObject as the EventSystem and InputSystemUIInputModule components. Something like the following:
    Code (CSharp):
    1. [RequireComponent(typeof(InputSystemUIInputModule))]
    2. [RequireComponent(typeof(EventSystem))]
    3. public class InputConfigurator : MonoBehaviour
    4. {
    5.     void Awake()
    6.     {
    7.         EventSystem.SetUITookitEventSystemOverride(
    8.             this.GetComponent<EventSystem>(),
    9.             sendEvents: true,
    10.             createPanelGameObjectsOnStart: true);
    11.     }
    12. }
     
  2. Onigiri

    Onigiri

    Joined:
    Aug 10, 2014
    Posts:
    502
    You sure it's working? Does nothing for me
     
  3. beevik_

    beevik_

    Joined:
    Sep 27, 2020
    Posts:
    108
    It works for me. But in order to make it work you also have to be careful about setting the picking mode of non-interactive elements to "Ignore" (instead of the default of "Position").

    There are all sorts of pitfalls in UI tookit that make it hard to prevent various elements from stealing focus.
     
    Onigiri likes this.