Prevent multiple UI elements receiving input from different pointers?

Hey there!

I’ve recently realized that independent Unity UI components can receive simultaneous inputs from different pointers.

  • Eg: 2 separate ScrollRects can be scrolled simultaneously by 1 finger each (on touchscreens).

This can lead to some very undesirable behavior in the application that I’m currently building.

Is there a way to ensure only 1 element can be, at any given time, deemed as the target for input events in the Unity UI system?

  • i.e., if the user has got a finger down on a ScrollRect, make sure only that ScrollRect receives all inputs until it is “released” by the user?

My app utilizes multitouch gestures such as pinch, so setting Input.multiTouchEnabled = false is not a feasible workaround to this problem.

Answering my own question here:

I went through the code for the Standalone Input Module (thank you, Unity, for making it open source) and found out that it would not restrict the receiver of multiple simultaneous executions for touches. So I wrote up my own custom Standalone Input Module that would allow you to choose whether or not you would like to allow multiple simultaneous receivers.

I’ve attached the code [113823-customstandaloneinputmodule.zip|113823], if anyone would like to use it in their projects.

How to use:

Just replace the current Standalone Input Module on your EventSystem GameObject with this script.

113820-customstandaloneinputmodule.png

You can set “allowMultipleSimultaneousReceivers” to true, if you want the default behaviour (multiple objects can be activated by multiple touches), or to false, if you want only one object to receive all inputs at any given time.

Cheers! :slight_smile:

Note that you may hit situations where ‘totalTouchPointerCount’ (in your provided .cs example) will become invalid - e.g. on newer iPhones after resuming the app. So you may also want something like:

private void OnApplicationFocus(bool hasFocus)
{
  if (hasFocus)
  {
    totalTouchPointerCount = 0;
    m_CurrentTouchFocusedGameObject = null;
  }
}