按钮触摸和屏幕触摸的区别
Differentiate between button touch and screen touch
我正在 中制作简单的游戏,玩家可以触摸屏幕上的任意位置来做某事。这是一种反射游戏。同时我在屏幕上有一个允许暂停游戏的按钮。问题是,当我触摸一个按钮时,游戏会同时检测到屏幕触摸,我想避免这种情况。我使用了一个在按下按钮时更改的布尔值,但它仍然无法正常工作 - 在检测到屏幕触摸后,游戏暂停了几分之一秒。
我的暂停游戏代码:
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
}
}
那么有没有办法在它检测到屏幕触摸之前更改 isPaused 布尔值?
检查你的触摸是在 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
}
我正在 中制作简单的游戏,玩家可以触摸屏幕上的任意位置来做某事。这是一种反射游戏。同时我在屏幕上有一个允许暂停游戏的按钮。问题是,当我触摸一个按钮时,游戏会同时检测到屏幕触摸,我想避免这种情况。我使用了一个在按下按钮时更改的布尔值,但它仍然无法正常工作 - 在检测到屏幕触摸后,游戏暂停了几分之一秒。
我的暂停游戏代码:
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
}
}
那么有没有办法在它检测到屏幕触摸之前更改 isPaused 布尔值?
检查你的触摸是在 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
}