简体   繁体   English

Unity Touch Count 忽略 Ui 输入

[英]Unity Touch Count ignore Ui input

I have a simple game where when user touches the screen, player jumps.我有一个简单的游戏,当用户触摸屏幕时,玩家会跳跃。 I have implemented this using touchCount as when touchCount =1 then player jumps.我已经使用 touchCount 实现了这一点,因为当 touchCount =1 然后玩家跳跃时。 But the problem is that there is a button on the screen so when user presses a button, touchCount is validated and player jumps.但问题是屏幕上有一个按钮,所以当用户按下按钮时,touchCount 被验证并且玩家跳转。 So how to enable player jump only when user is touching the non ui part of the screen.那么如何仅在用户触摸屏幕的非 ui 部分时才启用播放器跳转。 Thanks in advance.提前致谢。

This is for android right?>这是给 android 的吧?>

I'd recommend using ui buttons built in to unity to define the active position, this should autometically work for android I think.我建议使用统一内置的 ui 按钮来定义活动的 position,我认为这应该自动适用于 android。

or u could make an if statement for the position of the touch so if the position is lower or higher than a certain point it will not activate.或者你可以为触摸的 position 做一个 if 语句,所以如果 position 低于或高于某个点,它将不会激活。

You can add a check if the touch is happening on UI using EventSystem.current.IsPointerOverGameObject您可以使用EventSystem.current.IsPointerOverGameObject添加检查是否在 UI 上发生触摸

Example usage from the API: API 的示例用法:

// Check if there is a touch
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
    // Check if finger is over a UI element
    if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    {
        Debug.Log("Touched the UI");
    }
}

Using Linq Where you can then use this condition as a filter in order to only take into account those touches that are not over UI using eg使用Where然后您可以在其中将此条件用作过滤器,以便仅考虑那些未通过 UI 使用的触摸,例如

// Get all touches that are NOT over UI
var validTouches = Input.touches.Where(touch => !EventSystem.current.IsPointerOverGameObject(touch.fingerId)).ToArray();

// This is basically a shortcut for writing something like
//var touchesList = new List<Touch>();
//foreach(var touch in Input.touches)
//{
//    if(!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
//    {
//         touchesList.Add(touch);
//    } 
//}
//var validTouches = touchesList.ToArray();

if(validTouches.Length == 1)
{
    // Your jump here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM