让UI对象以5为单位吞咽触摸的最优雅方法是什么? [英] What is the most elegant way to let UI objects swallow touches in unity 5?

查看:80
本文介绍了让UI对象以5为单位吞咽触摸的最优雅方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏使用带有画布的统一5新UI系统.游戏本身可以通过函数OnMouseDown()在具有指示可触摸区域的2D对撞机的多个游戏对象上接收弹药射击的触摸,并且我可以通过更改游戏对象的位置来调整不同可触摸区域的优先级.

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.

但是,添加UI后,对UI元素(按钮,面板等)的任何触摸不仅会(如果可能)触发UI元素,而且还可以通过UI元素并触发可触摸区域.当您按下一个按钮时,这很奇怪,不仅按下了按钮,而且还触发了(UI)"UI层"后面的射击动作.

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".

我可以想到一种方法是向UI元素添加对撞机,然后在运行时将其位置和大小更改为世界空间,并调整其position.z值以吞并UI上的所有触摸.但是,这看起来非常丑陋且不安全.

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.

是否有任何优雅的方法可以让所有UI元素(基本上是面板)吞噬触摸?谢谢!

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

推荐答案

您可以在相机上使用2d或3d物理光线投射器来完成此操作. BoredMormon有一个很棒的5分钟视频,介绍如何执行此操作,我建议您查看一下! https://www.youtube.com/watch?v=EVZiv7DLU6E

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

这篇关于让UI对象以5为单位吞咽触摸的最优雅方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆