[Android] Fix touch input with the new input system. #938
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
Fixes #766
Unity code or UI that uses touch events using the (new) Input System package doesn't work when using this widget with the default settings on Android.
Flutter has 2 embedding modes that work for this plugin on Android:
useAndroidViewSurface: false
useAndroidViewSurface: true
Touch input using the new input system currently works in HC mode, but not in VD mode.
This PR adds a workaround to make touch input work for both modes when using the new Input System.
Context
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.Changing the deviceId does not make any difference to the old input system, which likely ignores it anyway.
Testing
Input System
package using the package manager.Active Input Handling
toInput System Package (New)
Unity
So far I've tested this on Unity 2022.3.21, 2021.3.35 and 2019.4.40.
Android
It works on various Android devices and versions:
learntoflutter/flutter_embed_unity#17 (comment)
Video
Comparison of touch input before and after this fix. Using
useAndroidViewSurface: false
and the new input system.Before.mp4
After.mp4
Type of Change