Reputation: 5566
Suppose I have a game where the whole screen is interactable aside from a few UI buttons / elements (i.e. the screen-wide touch handler should be called when touches occur anywhere outside the UI buttons, but not be called when the touch occurs on the UI buttons, since touches occuring on the UI bottons should call the button handler, not the screen-wide touch handler)
What is the best way to block the touch events when the touch occurs on the UI elements?
I've seen multiple approaches...
Update()
function, check EventSystem.current.IsPointerOverGameObject(touch.fingerId)
and ignore if it is not falseThis seems to be most straight forward and flexible since the Update()
function can use the Input
object directly
OnPointerEnter
, OnPointerClick
, etc. to handle the touch eventsThis seems to be the recommended approach under the new GUI system but appears to be less flexible since the raw Input
values is shielded by the PointerEventData
, in particular, handling multitouch seems to be tricky with this approach (Also the use of a stretched invisible image at the background of the canvas seems hacky...)
Since this seems to be an incredibly common thing to do, I'd like to know the industry common practice of such a problem.
Thanks
Related links:
http://answers.unity3d.com/questions/784617/how-do-i-block-touch-events-from-propagating-throu.html https://www.youtube.com/watch?v=EVZiv7DLU6E
Upvotes: 0
Views: 1264
Reputation: 61
I am not competent to help in gaming development but would like to offer an idea for consideration:
Upvotes: 0
Reputation: 10750
I have never seen such use case of screen-wide touch which goes beyond the methods described in your question. According to natural mapping of interaction, If you have multi-touch interaction model, you should have enough space on screen that user can easily interact without getting close to UI items. Otherwise its not a good interface implementation. So you can use update()
(method 1 in the OP) to detect tap, swipe, drag and pinch etc. And if you just need single touch (tap) interaction then method 2 is recommended, add a background transparent panel and register onclick()
listner. So it all depend on your interaction model's requirements.
Upvotes: 1