Skip to content

Commit b9177c1

Browse files
authored
Fix new input system touches on android.
1 parent ecee37d commit b9177c1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

android/src/main/kotlin/com/xraph/plugin/flutter_unity_widget/CustomUnityPlayer.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@ class CustomUnityPlayer(context: Activity, upl: IUnityPlayerLifecycleEvents?) :
4646
if (event == null) return false
4747

4848
event.source = InputDevice.SOURCE_TOUCHSCREEN
49-
return super.onTouchEvent(event)
49+
50+
// true for Flutter Virtual Display, false for Hybrid composition.
51+
if (event.deviceId == 0) {
52+
/*
53+
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)
54+
Unity's new Input System package does not detect these touches, copy the motion event to change the immutable deviceId.
55+
*/
56+
val modifiedEvent = event.copy(deviceId = -1)
57+
event.recycle()
58+
return super.onTouchEvent(modifiedEvent)
59+
} else {
60+
return super.onTouchEvent(event)
61+
}
5062
}
5163

5264
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// source https://gist.github.com/sebschaef/b803da53217c88e8c691aeed08602193
2+
3+
package com.xraph.plugin.flutter_unity_widget
4+
5+
import android.view.MotionEvent
6+
7+
/*
8+
Copies a MotionEvent. Use the named parameters to modify immutable properties.
9+
Don't forget to recycle the original event if it is not used anymore.
10+
*/
11+
fun MotionEvent.copy(
12+
downTime: Long = getDownTime(),
13+
eventTime: Long = getEventTime(),
14+
action: Int = getAction(),
15+
pointerCount: Int = getPointerCount(),
16+
pointerProperties: Array<MotionEvent.PointerProperties>? =
17+
(0 until getPointerCount())
18+
.map { index ->
19+
MotionEvent.PointerProperties().also { pointerProperties ->
20+
getPointerProperties(index, pointerProperties)
21+
}
22+
}
23+
.toTypedArray(),
24+
pointerCoords: Array<MotionEvent.PointerCoords>? =
25+
(0 until getPointerCount())
26+
.map { index ->
27+
MotionEvent.PointerCoords().also { pointerCoords ->
28+
getPointerCoords(index, pointerCoords)
29+
}
30+
}
31+
.toTypedArray(),
32+
metaState: Int = getMetaState(),
33+
buttonState: Int = getButtonState(),
34+
xPrecision: Float = getXPrecision(),
35+
yPrecision: Float = getYPrecision(),
36+
deviceId: Int = getDeviceId(),
37+
edgeFlags: Int = getEdgeFlags(),
38+
source: Int = getSource(),
39+
flags: Int = getFlags()
40+
): MotionEvent =
41+
MotionEvent.obtain(
42+
downTime,
43+
eventTime,
44+
action,
45+
pointerCount,
46+
pointerProperties,
47+
pointerCoords,
48+
metaState,
49+
buttonState,
50+
xPrecision,
51+
yPrecision,
52+
deviceId,
53+
edgeFlags,
54+
source,
55+
flags
56+
)

0 commit comments

Comments
 (0)