What is the most elegant way to let UI objects swallow touches in unity 5?
Asked Answered
S

1

4

My game uses unity 5 new UI system with canvas. The game itself can receive touches to shoot ammos via functions OnMouseDown() on several game objects with 2D colliders indicating touchable areas, and I can adjust the priorities of different touchable areas by changing the game objects' position.z.

However with the UI added, any touches on UI elements (buttons, panels, etc.) will not only (if possible) trigger the UI elements, but they can also pass through UI elements and trigger the touchable areas. It's very weird when you press a button, not only the button is pressed, but also triggers the shooting action behind (visually) the "UI layer".

I can think of one way is to add a collider to UI elements, then in the runtime change its position and size into world space, and adjust it's position.z value to swallow all touches that are on UI. However this seems very ugly and unsafe.

Is there any elegant way to let all UI elements (basically panels) to swallow touches? Thanks!

Soniferous answered 13/2, 2016 at 14:41 Comment(3)
this is a billion times duplicate on the internet. You could have saved huge time by just searching. it's the most fundamental, everyday, problem in all of Unity. answers.unity3d.com/questions/784617/…Metamorphosis
Possible duplicate of Registering Touch on UI in UnityMetamorphosis
@Joe Blow Thanks a lot for the very helpful link! I did some researches before raising up the question, but failed to find any up-to-date and useful information. Either because my English sucks, or my searching skill sucks. Maybe both XDSoniferous
G
1

You can just use a 2d or 3d physics raycaster on the camera to accomplish this. BoredMormon has a great 5-min video on how to do this, I'd recommend checking it out! https://www.youtube.com/watch?v=EVZiv7DLU6E

Grievance answered 13/2, 2016 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.