c# - 手机,如何检查被触摸的东西?
问题描述
我目前正在尝试在 Unity 中做一个答题器游戏,我有以下情况:
我在屏幕上有多个按钮。目前(就像几乎所有的点击游戏一样)你按下屏幕就可以了。问题是,如果有人单击该按钮,它还会运行仅在按下“常规”屏幕时才应执行的逻辑。
目前我正在使用:
if (Input.GetMouseButtonDown(0))
但我需要知道如何在触摸按钮时“过滤掉”。
解决方案
您可以有一个触摸条件,如果您在具有 ScreenToWorldPoint 的 Camera 定义的某个区域中按下,则该条件不满足。我使用触摸并检查屏幕上是否有触摸。如果它们低于世界中的 y 坐标,则条件不满足,您可以在此处使用按钮:
public void Update(){
TouchInput();
}
public void TouchInput(){
if(touch.tapCount == 1 && TouchCondition(touch))
{
/*Here you put your function which is used when no buttons are pressed. In my
example
the Game starts. */
StartGame();
}
}
public bool TouchCondition(Touch touch)
{
/*You put your Condition into this if statement below.
In my example, if the touch is higher than the 2 y-coordinate,
than the condition is fulfilled and you start the game.
Otherwise it is not fulfilled and you can have all the buttons below 2y-coordinate */
if (Camera.main.ScreenToWorldPoint(touch.position).y >= 2)
{
return true;
}
else
{
return false;
}
}
推荐阅读
- php - Remove the Header and Footer on WooCommerce My-Account for unlogged users
- python - Do we must make a complex query in PySpark or a simple, and use .filter / .select?
- php - ora-03113-end-of-file-on-communication-channel 错误来自 Windows 10 上 docker 中的 php
- reactjs - 反应 setState 不适用于 websocket
- javascript - 有办法将“.then 回调”与 useEffect 一起使用吗?
- react-native - React Native 用户登录问题(AsyncStorage)
- angular - Angular 9 中的环境实现
- java - 从非活动类调用 MainActivity 类的方法
- base64 - 是否可以从八位字节流中提取文件名?
- python - How to drop rows in a data frame having any entry with the value zero ? ('collections.OrderedDict' object has no attribute 'dropna')