Is there a way to prevent Android touches to pass through UI elements on a canvas? I’m using Unity 5.2.1f1.
Possible answers for non-tile map uses
Setup
- 2d game.
- An overlay canvas with
buttons. - A gameobject input handler
that checks for world input on the
update method. Input through this
method checks the world for
interactivity.
Why do I check for world objects based at a location? I’m using a tile map that dynamically loads tiles and the system has a simple method to convert a world location to a specific tile location on the map. The tiles do not have any events on them.
Desktop and Editor Work
Grab from the update method Input.mousePosition if the pointer is not over a game object.
if( EventSystem.current.IsPointerOverGameObject() )
return;
Android Build Doesn’t Work
Grab the first touch. If it has ended check if the pointer is over an object.
Touch touch = Input.GetTouch(0);
if( touch.phase == TouchPhase.Ended )
{
//prevent touch through
if( EventSystem.current.IsPointerOverGameObject(touch.fingerId) )
return false;
vec = touch.position;
return true;
}
I’ve also tried looping over touches. IsPointerOverGameObject appears to be useless for preventing Android touches from passing through UI canvas objects.
Android Build Work Around
The only way I’ve found how to work around this issue, thanks to Fattie, is to check for a selected gameobject in the event system.
if( EventSystem.current.currentSelectedGameObject != null ) return false;
Is there a better way to do this? The only tutorials I’ve seen that might relate require an event interface for the tiles in the tile map and any game object agents acting “on top” of the tile map.
I’ve spent about my whole afternoon reading forum posts about this issue and have not come way with a better solution.
Some discussions
http://forum.unity3d.com/threads/ui-not-working-5-1-1-5-2-0b1.339462/#post-2195414
http://forum.unity3d.com/threads/upgrading-to-unity-5-2-ui-problem-with-raycast-target.353586/