- Home /
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.
Answer by RahulOfTheRamanEffect · Mar 27, 2018 at 08:17 PM
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 here, 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.
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! :)
Answer by simona_exient · Feb 11 at 02:07 PM
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;
}
}
Your answer
Follow this Question
Related Questions
EventSystem - Broadcast my own Input 1 Answer
Can I Simulate the "Submit" Event with Gamepad Buttons? 0 Answers
IPointerEnterHandler doesn't work when object switches parent 1 Answer
Input registering random touches 1 Answer
How to get UI elements such as Scroll Rect to work with new input system? 0 Answers