- Home /
How do I intercept touch events in Android native code?
I am developing an Android plugin (video player). I need to hide UnityPlayer without pausing it and put my own VideoView instead of it. The only way I was able to do that was moving UnityPlayer off-screen:
public class Activity extends UnityPlayerActivity {
private int defaultLeftMargin = 0;
private boolean visible = true;
void setVisibility(final boolean visible) {
this.visible = visible;
MarginLayoutParams lp = (MarginLayoutParams) mUnityPlayer.getLayoutParams();
lp.leftMargin = visible ? defaultLeftMargin : 100500;
mUnityPlayer.requestLayout();
}
}
However, UnityPlayer still consumes all my touch events (a click on Unity's GUI.Button (new Rect (0, 0, 150, 150), created in OnGUI(), is triggered, despite the whole player being off-screen).
Which View handles all the events? Does it use onTouchEvent() or dispatchTouchEvent()? How can I intercept those events?
I have tried adding my own Views with Activity::addContentView(), but Unity continued handling touches.
I don't think Unity is gonna give you the control you want. requestLayout() doesn't guarantee your parent layout anyway. That entirely depends on what the parent sets and I'm not sure if the Unity framework for Android works like that. I think what you need to do should be done natively.
The answer appeared to be documented, adding tag meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true"
in the manifest helped me.
Thank you! This was driving me crazy. Can you provide a link to the documentation that led you to this?