juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.09k stars 504 forks source link

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

Closed timbotimbo closed 1 week ago

timbotimbo commented 3 months ago

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:

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())

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

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

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