c# - 按键触摸和屏幕触摸的区别

标签 c# button unity-game-engine touch

我正在制作简单的游戏,玩家可以触摸屏幕上的任何位置来执行某些操作。这是一种反射游戏。同时,屏幕上有一个按钮可以暂停游戏。问题是,当我触摸按钮时,游戏会同时检测到屏幕触摸,我想避免这种情况。我使用了一个 bool 值,在按下按钮时更改它,但它仍然无法正常工作 - 检测到屏幕触摸后,游戏仅暂停了几分之一秒。

我的暂停游戏代码:

GameControlerScript:

public static bool isPaused;
public void PauseGame()
    {
        isPaused = true; // this is static
        Time.timeScale = 0.0f;

        //more code here

    }

以及我在附加到不同对象的脚本中的触摸检测:

void Update()
    {

        if (((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetKeyDown(KeyCode.Space))) && !GameControllerScript.isPaused)
        {
             // code here starts to be executed and then pasues because of timescale
        }

    }

那么有没有办法在检测到 scrren 触摸之前对 isPaused bool 值进行更改?

最佳答案

检查您的触摸是否在 UI 元素上,如果没有调用您为正常输入调用的任何方法

public void Update()

{
    foreach (Touch touch in Input.touches)
    {
        if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            // you touched at least one UI element
            return;
        }
    }

    // you didnt touched any UI element
    // Do Something
}

关于c# - 按键触摸和屏幕触摸的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754676/

相关文章:

c# - 以编程方式向滚动查看器添加内容,滚动条停止工作

c# - linq Expression<TDelegate> 赋值如何在语言语法级别上工作

c# - 当所有其他对象暂停时移动角色

c# - 在 catch block 中调用异步方法

c# - 以编程方式添加数据注释

javascript - 选中复选框时删除按钮可见

css - 限制CSS中按钮的宽度

css - 波纹效果按钮 CSS3

unity-game-engine - Unity 中的导航网格是什么?

javascript - Apache 未加载 WebGL 内容