Skip to content

Commit

Permalink
Fix new input system touches on android.
Browse files Browse the repository at this point in the history
  • Loading branch information
timbotimbo committed Jun 23, 2024
1 parent 1e21706 commit 0abef50
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

}
Original file line number Diff line number Diff line change
@@ -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<MotionEvent.PointerProperties>? =
(0 until getPointerCount())
.map { index ->
MotionEvent.PointerProperties().also { pointerProperties ->
getPointerProperties(index, pointerProperties)
}
}
.toTypedArray(),
pointerCoords: Array<MotionEvent.PointerCoords>? =
(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
)

0 comments on commit 0abef50

Please sign in to comment.