- Home /
GUI occlusion for TouchScript on Android
I'm using Unity 5.0.2 (free).
The game I'm working on mixes Unity's Canvas-based GUI elements with 3D elements -- picture a star map that can be panned, rotated, and zoomed, with a screen-space scrollable list of stars on one side.
To avoid moving the map when scrolling through the list, I created a new TouchScript Layer, based on FullscreenLayer.cs. In the Hit method I replaced the "did I hit an object" code with this:
EventSystem eventSystem = UnityEngine.EventSystems.EventSystem.current;
if (eventSystem.IsPointerOverGameObject()) {
Debug.Log("Generic gui hit: " + position);
hit = null;
return LayerHitResult.Hit;
}
The Layer takes ownership of the touch by returning "Hit", but puts a null value in the out parameter that would identify the touched object. The net result is that any touch or mouse click that begins on a GUI element is ignored by TouchScript.
That worked fine on Windows, but not on Android, where IsPointerOverGameObject() never returned true. After digging through the docs for a bit, I speculatively added this:
TouchInputModule tim = eventSystem.GetComponent<TouchInputModule>();
if (tim.IsPointerOverGameObject(0)) {
Debug.Log("Touch gui hit: " + position);
hit = null;
return LayerHitResult.Hit;
}
This actually worked, and the behavior is exactly what I wanted: click or click-drag on GUI elements has no effect on the map, and two-finger gestures like pinch-to-zoom track across GUI elements so long as your initial touch is outside them.
My question is: why? Why was the explicit call into the TouchInputModule needed? What does the "pointerId" argument (0) mean? Is there a better way to do this? The reference documentation is a bit thin in this area.
I'm happy that I have a solution, but I worry that it'll break in the future.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
android input problem? 0 Answers
Android touch gui help!!! 2 Answers
GUI.Button Touch Input Problem 1 Answer
Main menu touch input for android 2 Answers
Button reaction time problem 1 Answer