Skip to content
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

Fix new input system touches on android. #958

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Loading