我正在制作简单的游戏,玩家可以触摸屏幕上的任何位置来执行某些操作。这是一种反射游戏。同时,屏幕上有一个按钮可以暂停游戏。问题是,当我触摸按钮时,游戏会同时检测到屏幕触摸,我想避免这种情况。我使用了一个 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/