Fix new input system touches on android. #17
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Unity code or UI that uses touch events using the (new) Input System package doesn't work when using this widget.
This PR adds a workaround to make touch input work using the new Input System.
Context
I've been experimenting and this does work in a platform view using Hybrid composition, it is just Virtual display that is broken.
After logging both implementations I noticed that touch events using HC have a positive integer
deviceId
, which can be anything for different devices.VD touch events will always have a deviceId of 0.
According to the android docs
My tests on multiple real devices and emulators showed me values of 2, 3, 4, 12 and 14.
Fix
In
onToucEvent
change the deviceId of a MotionEvent if it is 0.I chose
-1
as that works in Unity and seems unlikely to conflict with any actual hardware.The only downside is that we need to copy the full MotionEvent because the deviceId is immutable.
However I believe performance impact is negilible.
Changing the deviceId does not make any difference to the old input system, which likely ignores it anyway.
Testing
So far I've tested this with Unity 2022.3.21 on:
Video
I should probably have used a bigger button, but you can see the fix in these screen recordings.
before.mp4
after.mp4
Just to be sure this PR might need some additional testing on different Android devices and Unity versions.
However I'm confident that
deviceId = -1
will be safe for nearly anyone.