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.
An id of zero indicates that the event didn't come from a physical device and maps to the default keymap. The other numbers are arbitrary and you shouldn't depend on the values.
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
Use the example Unity project.
Install the Input System package using the package manager.
In the player settings, set Active Input Handling to Input System Package (New)
On the EventSystem in the scene, make sure the new input system is used.
Run the flutter app and select Simple Unity Demo.
Try pressing the UI buttons.
It doesn't work without this fix.
Unity
So far I've tested this on Unity 2022.3.21, 2021.3.35 and 2019.4.40.
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](https://developer.android.com/reference/android/view/MotionEvent#getDeviceId())
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:
https://github.com/learntoflutter/flutter_embed_unity/pull/17#issuecomment-1995264765
Video
Comparison of touch input before and after this fix. Using
useAndroidViewSurface: false
and the new input system.Type of Change