首页 > 解决方案 > 手机,如何检查被触摸的东西?

问题描述

我目前正在尝试在 Unity 中做一个答题器游戏,我有以下情况:

我在屏幕上有多个按钮。目前(就像几乎所有的点击游戏一样)你按下屏幕就可以了。问题是,如果有人单击该按钮,它还会运行仅在按下“常规”屏幕时才应执行的逻辑。

目前我正在使用:

if (Input.GetMouseButtonDown(0))

但我需要知道如何在触摸按钮时“过滤掉”。

标签: c#unity3d

解决方案


您可以有一个触摸条件,如果您在具有 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;
                }
            }

推荐阅读