Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一触摸计数忽略用户界面输入_C#_Unity3d_Game Development - Fatal编程技术网

C# 统一触摸计数忽略用户界面输入

C# 统一触摸计数忽略用户界面输入,c#,unity3d,game-development,C#,Unity3d,Game Development,我有一个简单的游戏,当用户触摸屏幕时,玩家会跳起来。我已经用touchCount实现了这一点,因为当touchCount=1时,玩家就会跳转。但问题是屏幕上有一个按钮,所以当用户按下按钮时,touchCount被验证,玩家跳转。所以,如何仅在用户触摸屏幕的非ui部分时启用播放器跳转。提前谢谢。这是android版的,对吗?> 我建议使用unity内置的ui按钮来定义活动位置,我认为这会自动适用于android 或者你可以为触摸的位置做一个if语句,这样如果位置低于或高于某个点,它将不会激活。你可

我有一个简单的游戏,当用户触摸屏幕时,玩家会跳起来。我已经用touchCount实现了这一点,因为当touchCount=1时,玩家就会跳转。但问题是屏幕上有一个按钮,所以当用户按下按钮时,touchCount被验证,玩家跳转。所以,如何仅在用户触摸屏幕的非ui部分时启用播放器跳转。提前谢谢。

这是android版的,对吗?>

我建议使用unity内置的ui按钮来定义活动位置,我认为这会自动适用于android


或者你可以为触摸的位置做一个if语句,这样如果位置低于或高于某个点,它将不会激活。

你可以使用

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");
    }
}
然后,您可以使用此条件作为过滤器,以便仅考虑那些未通过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
}
//获取所有未通过UI的触摸
var validTouches=Input.touchs.Where(touch=>!EventSystem.current.IsPointerOverGameObject(touch.fingerId)).ToArray();
//这基本上是一个写类似这样东西的捷径
//var touchesList=新列表();
//foreach(Input.touch中的var touch)
//{
//如果(!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
//    {
//触摸列表。添加(触摸);
//    } 
//}
//var validTouches=touchesList.ToArray();
if(validTouches.Length==1)
{
//你跳这里
}

这不会解决问题,因为每个设备都有自己的分辨率。对一台设备执行上述操作对其他设备不会有好处。我建议使用unityUi按钮内置的Ui按钮进行跳转?是的,使按钮运行一个功能来跳转链接中触及的按钮,但最好提及需要使用System.Linq在顶部。