diff --git a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/CustomUnityPlayer.kt b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/CustomUnityPlayer.kt index a9dc7abb..a550ad92 100755 --- a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/CustomUnityPlayer.kt +++ b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/CustomUnityPlayer.kt @@ -46,7 +46,19 @@ class CustomUnityPlayer(context: Activity, upl: IUnityPlayerLifecycleEvents?) : if (event == null) return false event.source = InputDevice.SOURCE_TOUCHSCREEN - return super.onTouchEvent(event) + + // true for Flutter Virtual Display, false for Hybrid composition. + if (event.deviceId == 0) { + /* + Flutter creates a touchscreen motion event with deviceId 0. (https://github.com/flutter/flutter/blob/34b454f42dd6f8721dfe43fc7de5d215705b5e52/packages/flutter/lib/src/services/platform_views.dart#L639) + Unity's new Input System package does not detect these touches, copy the motion event to change the immutable deviceId. + */ + val modifiedEvent = event.copy(deviceId = -1) + event.recycle() + return super.onTouchEvent(modifiedEvent) + } else { + return super.onTouchEvent(event) + } } } \ No newline at end of file diff --git a/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/utils/CopyMotionEvent.kt b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/utils/CopyMotionEvent.kt new file mode 100644 index 00000000..48580378 --- /dev/null +++ b/android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/utils/CopyMotionEvent.kt @@ -0,0 +1,56 @@ +// source https://gist.github.com/sebschaef/b803da53217c88e8c691aeed08602193 + +package com.xraph.plugin.flutter_unity_widget + +import android.view.MotionEvent + +/* + Copies a MotionEvent. Use the named parameters to modify immutable properties. + Don't forget to recycle the original event if it is not used anymore. +*/ +fun MotionEvent.copy( + downTime: Long = getDownTime(), + eventTime: Long = getEventTime(), + action: Int = getAction(), + pointerCount: Int = getPointerCount(), + pointerProperties: Array? = + (0 until getPointerCount()) + .map { index -> + MotionEvent.PointerProperties().also { pointerProperties -> + getPointerProperties(index, pointerProperties) + } + } + .toTypedArray(), + pointerCoords: Array? = + (0 until getPointerCount()) + .map { index -> + MotionEvent.PointerCoords().also { pointerCoords -> + getPointerCoords(index, pointerCoords) + } + } + .toTypedArray(), + metaState: Int = getMetaState(), + buttonState: Int = getButtonState(), + xPrecision: Float = getXPrecision(), + yPrecision: Float = getYPrecision(), + deviceId: Int = getDeviceId(), + edgeFlags: Int = getEdgeFlags(), + source: Int = getSource(), + flags: Int = getFlags() +): MotionEvent = + MotionEvent.obtain( + downTime, + eventTime, + action, + pointerCount, + pointerProperties, + pointerCoords, + metaState, + buttonState, + xPrecision, + yPrecision, + deviceId, + edgeFlags, + source, + flags + ) \ No newline at end of file