flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

[Android] Fix touch input with the new input system.

Open timbotimbo opened this issue 1 year ago โ€ข 0 comments

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:

  • Virtual Display (default), useAndroidViewSurface: false
  • Hybrid Composition, 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

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. eventsystem
  • 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.

Android

It works on various Android devices and versions:

  • Samsung S20FE, Android 13
  • Samsung S8, Android 9,
  • Wilefox Swift, Android 7,
  • Samsung J5 Android 6,

https://github.com/learntoflutter/flutter_embed_unity/pull/17#issuecomment-1995264765

I've tested using 2022.3.20 using the devices I have available, all looks good:

Pixel 2 Galaxy S22 Galaxy Tab A7 Xiaomi 10T Pro

Video

Comparison of touch input before and after this fix. Using useAndroidViewSurface: false and the new input system.

https://github.com/juicycleff/flutter-unity-view-widget/assets/11444698/fa9c2644-e014-4b67-a580-5cdd15f5add5

https://github.com/juicycleff/flutter-unity-view-widget/assets/11444698/9055958e-154c-4ff4-af6b-45eca95168b9

Type of Change

  • [ ] โœจ New feature (non-breaking change which adds functionality)
  • [x] ๐Ÿ› ๏ธ Bug fix (non-breaking change which fixes an issue)
  • [ ] โŒ Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] ๐Ÿงน Code refactor
  • [ ] โœ… Build configuration change
  • [ ] ๐Ÿ“ Documentation
  • [ ] ๐Ÿ—‘๏ธ Chore

timbotimbo avatar Mar 19 '24 10:03 timbotimbo