Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed

Conversation

timbotimbo
Copy link
Collaborator

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,

learntoflutter/flutter_embed_unity#17 (comment)

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.

Before.mp4
After.mp4

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unity's New Input system breaks touch input on android.
1 participant