Skip to content

Fix new input system touches on android. #17

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

Merged
merged 1 commit into from
Mar 13, 2024

Conversation

timbotimbo
Copy link
Collaborator

@timbotimbo timbotimbo commented Mar 13, 2024

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

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.

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

  • Use the example project.
  • Add a canvas with a button. Make sure the eventsystem uses the new input system.
    canvas
    eventsystem
  • Now try pressing the canvas button while running the Flutter app.
  • It doesn't work without this fix.

So far I've tested this with Unity 2022.3.21 on:

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

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.

@jamesncl
Copy link
Collaborator

This looks great, thank you. I'm thinking this could be released as a pre-release version (something like 1.1.0-beta1)

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

@jamesncl jamesncl merged commit 6d96153 into learntoflutter:main Mar 13, 2024
@jamesncl
Copy link
Collaborator

Published as 1.1.0-beta1

dependencies:
  flutter_embed_unity: ^1.1.0-beta1

@timbotimbo
Copy link
Collaborator Author

I just tested and got it working using Unity 2019.3.40, which uses Input system 1.3 from 2022.
Since it works that far back, I don't think there will be compatibility issues based on Unity versions.

@jamesncl
Copy link
Collaborator

Great. I'll test it 'in the wild' on one of my apps in a beta track and then hopefully promote it to 1.1.0 production

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.

2 participants