Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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
在unity中的android上滑动手势_Android_Input_Unity3d_Touch_Gestures - Fatal编程技术网

在unity中的android上滑动手势

在unity中的android上滑动手势,android,input,unity3d,touch,gestures,Android,Input,Unity3d,Touch,Gestures,我试图让unity认识到我是从左向右滑动的,我已经解决了这个问题,但我的问题是,直到我把手指从屏幕上拿开,unity才明白这一点 我的问题是,我如何才能做到让它知道我在屏幕上不动手指的情况下,一次又一次地左右移动 这是我到目前为止的代码 using UnityEngine; using System.Collections; public class Gestures : MonoBehaviour { private Vector2 fingerStart; private Vector2

我试图让unity认识到我是从左向右滑动的,我已经解决了这个问题,但我的问题是,直到我把手指从屏幕上拿开,unity才明白这一点

我的问题是,我如何才能做到让它知道我在屏幕上不动手指的情况下,一次又一次地左右移动

这是我到目前为止的代码

using UnityEngine;
using System.Collections;

public class Gestures : MonoBehaviour {

private Vector2 fingerStart;
private Vector2 fingerEnd;

public int leftRight = 0;
public int upDown = 0;

void Update () {
    foreach(Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began)
        {
            fingerStart = touch.position;
            fingerEnd  = touch.position;
        }
        if (touch.phase == TouchPhase.Moved )
        {
            fingerEnd = touch.position;

        }
        if(touch.phase == TouchPhase.Ended)
        {
            if((fingerStart.x - fingerEnd.x) > 80 || (fingerStart.x - fingerEnd.x) < -80) // Side to side Swipe
            {
                leftRight ++;
            }
            else if((fingerStart.y - fingerEnd.y) < -80 || (fingerStart.y - fingerEnd.y) > 80) // top to bottom swipe
            {
                upDown ++;

            }
            if(leftRight >= 3){

                leftRight = 0;
            }
            if(upDown >= 4){

                upDown = 0;
            }
        }
    }
}
}
使用UnityEngine;
使用系统集合;
公共课堂手势:单一行为{
专用矢量2 fingerStart;
专用矢量2指端;
公共int leftRight=0;
公共int upDown=0;
无效更新(){
foreach(输入触摸,触摸)
{
if(touch.phase==TouchPhase.start)
{
fingerStart=touch.position;
fingerEnd=touch.position;
}
如果(touch.phase==TouchPhase.Moved)
{
fingerEnd=touch.position;
}
如果(touch.phase==TouchPhase.end)
{
如果((fingerStart.x-fingerEnd.x)>80 | |(fingerStart.x-fingerEnd.x)<-80)//侧对侧滑动
{
leftRight++;
}
如果((fingerStart.y-fingerEnd.y)<-80 | |(fingerStart.y-fingerEnd.y)>80)//从上到下滑动
{
upDown++;
}
if(leftRight>=3){
leftRight=0;
}
如果(向上向下>=4){
向上向下=0;
}
}
}
}
}

您面临的问题是因为您在TouchPhase.Ended中完成了检查。您要做的是在TouchPhase.Moved中执行检查,值的更改较小(您在End中使用80,如果代码不起作用,请尝试类似10的方法)

Unity关于TouchPhase的文档

foreach(在Input.Touch中触摸)
{
if(touch.phase==TouchPhase.start)
{
fingerStart=touch.position;
fingerEnd=touch.position;
}
如果(touch.phase==TouchPhase.Moved)
{
fingerEnd=touch.position;
如果((fingerStart.x-fingerEnd.x)>80 |
(fingerStart.x-fingerEnd.x)<-80)//左右滑动
{
leftRight++;
}
如果((fingerStart.y-fingerEnd.y)<-80 | |
(fingerStart.y-fingerEnd.y)>80)//从上到下滑动
{
upDown++;
}
if(leftRight>=3){
leftRight=0;
}
如果(向上向下>=4){
向上向下=0;
}
//执行检查后,将fingerStart和fingerEnd设置为相同
fingerStart=touch.position;
}
如果(touch.phase==TouchPhase.end)
{
leftRight=0;
向上向下=0;
fingerStart=矢量2.0;
fingerEnd=矢量2.0;
}



如果您想显式地检查模式(即左->右->左),而不是像代码那样只检查它是否是横向/垂直移动,请尝试下面的代码。只需记住包括System.Collentions.Generic和System.Linq名称空间

private Vector2 fingerStart;
private Vector2 fingerEnd;

public enum Movement
{
    Left,
    Right, 
    Up,
    Down
};

public List<Movement> movements = new List<Movement>();


void Update () {
    foreach(Touch touch in Input.touches)
    {

        if (touch.phase == TouchPhase.Began) {
            fingerStart = touch.position;
            fingerEnd  = touch.position;
        }

        if(touch.phase == TouchPhase.Moved) {
            fingerEnd = touch.position;

            //There is more movement on the X axis than the Y axis
            if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {

                //Right Swipe
                if((fingerEnd.x - fingerStart.x) > 0)
                    movements.Add(Movement.Right);
                //Left Swipe
                else
                    movements.Add(Movement.Left);

            }

            //More movement along the Y axis than the X axis
            else {
                //Upward Swipe
                if((fingerEnd.y - fingerStart.y) > 0)
                    movements.Add(Movement.Up);
                //Downward Swipe
                else
                    movements.Add(Movement.Down);
            }
            //After the checks are performed, set the fingerStart & fingerEnd to be the same
            fingerStart = touch.position;   

            //Now let's check if the Movement pattern is what we want
            //In this example, I'm checking whether the pattern is Left, then Right, then Left again
            Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
        }


        if(touch.phase == TouchPhase.Ended)
        {
            fingerStart = Vector2.zero;
            fingerEnd = Vector2.zero;
            movements.Clear();
        }
    }
}


private bool CheckForPatternMove (int startIndex, int lengthOfPattern, List<Movement> movementToCheck) {

    //If the currently stored movements are fewer than the length of the pattern to be detected
    //it can never match the pattern. So, let's get out
    if(lengthOfPattern > movements.Count)
        return false;

    //In case the start index for the check plus the length of the pattern
    //exceeds the movement list's count, it'll throw an exception, so lets get out
    if(startIndex + lengthOfPattern > movements.Count)
        return false;

    //Populate a temporary list with the respective elements
    //from the movement list
    List<Movement> tMovements = new List<Movement>();
    for(int i = startIndex; i < startIndex + lengthOfPattern; i++)
        tMovements.Add(movements[i]);

    //Now check whether the sequence of movements is the same as the pattern you want to check for
    //The SequenceEqual method is in the System.Linq namespace
    return tMovements.SequenceEqual(movementToCheck);
}
private Vector2 fingerStart;
专用矢量2指端;
公众普查运动
{
左边
正确的,
向上的
向下
};
公共列表移动=新列表();
无效更新(){
foreach(输入触摸,触摸)
{
if(touch.phase==TouchPhase.start){
fingerStart=touch.position;
fingerEnd=touch.position;
}
如果(touch.phase==TouchPhase.Moved){
fingerEnd=touch.position;
//X轴上的运动比Y轴上的运动多
if(Mathf.Abs(fingerStart.x-fingerEnd.x)>Mathf.Abs(fingerStart.y-fingerEnd.y)){
//右击
如果((fingerEnd.x-fingerStart.x)>0)
movements.Add(Movement.Right);
//左击
其他的
移动。添加(移动。左);
}
//沿Y轴的移动量大于沿X轴的移动量
否则{
//向上滑动
如果((fingerEnd.y-fingerStart.y)>0)
移动。添加(移动。向上);
//向下滑动
其他的
移动。添加(移动。向下);
}
//执行检查后,将fingerStart和fingerEnd设置为相同
fingerStart=touch.position;
//现在让我们检查一下运动模式是否符合我们的要求
//在本例中,我将检查模式是否为左、右、再左
Log(CheckForPatternMove(0,3,new List(){Movement.Left,Movement.Right,Movement.Left}));
}
如果(touch.phase==TouchPhase.end)
{
fingerStart=矢量2.0;
fingerEnd=矢量2.0;
动作。清除();
}
}
}
私有bool CheckForPatternMove(int startIndex、int lengthOfPattern、List movementToCheck){
//如果当前存储的移动小于要检测的图案长度
//它永远都不可能符合模式。所以,我们出去吧
如果(长度模式>移动数)
返回false;
//如果检查的开始索引加上图案的长度
//超过移动列表的计数,它将抛出异常,因此让我们退出
如果(开始索引+长度模式>移动数)
返回false;
//用相应的元素填充临时列表
//从移动列表中
List tMovements=新列表();
对于(int i=startIndex;iprivate Vector2 fingerStart;
private Vector2 fingerEnd;

public enum Movement
{
    Left,
    Right, 
    Up,
    Down
};

public List<Movement> movements = new List<Movement>();


void Update () {
    foreach(Touch touch in Input.touches)
    {

        if (touch.phase == TouchPhase.Began) {
            fingerStart = touch.position;
            fingerEnd  = touch.position;
        }

        if(touch.phase == TouchPhase.Moved) {
            fingerEnd = touch.position;

            //There is more movement on the X axis than the Y axis
            if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {

                //Right Swipe
                if((fingerEnd.x - fingerStart.x) > 0)
                    movements.Add(Movement.Right);
                //Left Swipe
                else
                    movements.Add(Movement.Left);

            }

            //More movement along the Y axis than the X axis
            else {
                //Upward Swipe
                if((fingerEnd.y - fingerStart.y) > 0)
                    movements.Add(Movement.Up);
                //Downward Swipe
                else
                    movements.Add(Movement.Down);
            }
            //After the checks are performed, set the fingerStart & fingerEnd to be the same
            fingerStart = touch.position;   

            //Now let's check if the Movement pattern is what we want
            //In this example, I'm checking whether the pattern is Left, then Right, then Left again
            Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
        }


        if(touch.phase == TouchPhase.Ended)
        {
            fingerStart = Vector2.zero;
            fingerEnd = Vector2.zero;
            movements.Clear();
        }
    }
}


private bool CheckForPatternMove (int startIndex, int lengthOfPattern, List<Movement> movementToCheck) {

    //If the currently stored movements are fewer than the length of the pattern to be detected
    //it can never match the pattern. So, let's get out
    if(lengthOfPattern > movements.Count)
        return false;

    //In case the start index for the check plus the length of the pattern
    //exceeds the movement list's count, it'll throw an exception, so lets get out
    if(startIndex + lengthOfPattern > movements.Count)
        return false;

    //Populate a temporary list with the respective elements
    //from the movement list
    List<Movement> tMovements = new List<Movement>();
    for(int i = startIndex; i < startIndex + lengthOfPattern; i++)
        tMovements.Add(movements[i]);

    //Now check whether the sequence of movements is the same as the pattern you want to check for
    //The SequenceEqual method is in the System.Linq namespace
    return tMovements.SequenceEqual(movementToCheck);
}
    //The idea of a pattern match is to check for the exact same set of swipe gesture.
    //This requires the following conditions to be met
    // (a) The List of movements that need to be checked must be at least as long as the List of movements to check against.
    // (b) The correct indices should be used for the startIndex. In this case I'm just using 0 as the startIndex.
    // (c) Remember to clear the List right after you get a true return from the method, otherwise the next return will most likely be a false. 

    //Example - Training set is Left -> Right -> Left (This is what we want to check)
    // Step 1 - User swipes LEFT, method returns false because there are too few Movements to check
    // Step 2 - User swipes RIGHT, method returns false (same reason as above)

    // Step 3a - User swipes RIGHT (L, R, R now) - false, incorrect pattern (L, R, R instead of L, R, L)
    // Step 3b - User swipes LEFT (L, R, L now) - TRUE, Correct pattern!

    //Immediately clear if Step 3b happens otherwise Step 4 will occur

    // Step 4 - User swipes L or R (direction is immaterial right now), and method will return FALSE
    // if you use the last three indexes!



    //Pre-populating the movements List with L, R, L
    movements = new List<Movement>()
    {
        Movement.Left,
        Movement.Right,
        Movement.Left
    };

    //Checking a match against an L, R, L training set
    //This prints true to the console
    Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left }  ));
void Update () {

    //Example usage in Update. Note how I use Input.GetMouseButton instead of Input.touch

    //GetMouseButtonDown(0) instead of TouchPhase.Began
    if (Input.GetMouseButtonDown(0)) {
        fingerStart = Input.mousePosition;
        fingerEnd  = Input.mousePosition;
    }

    //GetMouseButton instead of TouchPhase.Moved
    //This returns true if the LMB is held down in standalone OR
    //there is a single finger touch on a mobile device
    if(Input.GetMouseButton(0)) {
        fingerEnd = Input.mousePosition;

        //There was some movement! The tolerance variable is to detect some useful movement
        //i.e. an actual swipe rather than some jitter. This is the same as the value of 80
        //you used in your original code.
        if(Mathf.Abs(fingerEnd.x - fingerStart.x) > tolerance || 
           Mathf.Abs(fingerEnd.y - fingerStart.y) > tolerance) {

            //There is more movement on the X axis than the Y axis
            if(Mathf.Abs(fingerStart.x - fingerEnd.x) > Mathf.Abs(fingerStart.y - fingerEnd.y)) {
                //Right Swipe
                if((fingerEnd.x - fingerStart.x) > 0)
                    movements.Add(Movement.Right);
                //Left Swipe
                else
                    movements.Add(Movement.Left);
            }

            //More movement along the Y axis than the X axis
            else {
                //Upward Swipe
                if((fingerEnd.y - fingerStart.y) > 0)
                    movements.Add(Movement.Up);
                //Downward Swipe
                else
                    movements.Add(Movement.Down);
            }

            //After the checks are performed, set the fingerStart & fingerEnd to be the same
            fingerStart = fingerEnd;

            //Now let's check if the Movement pattern is what we want
            //In this example, I'm checking whether the pattern is Left, then Right, then Left again
            Debug.Log (CheckForPatternMove(0, 3, new List<Movement>() { Movement.Left, Movement.Right, Movement.Left } ));
        }
    }

    //GetMouseButtonUp(0) instead of TouchPhase.Ended
    if(Input.GetMouseButtonUp(0)) {
        fingerStart = Vector2.zero;
        fingerEnd = Vector2.zero;
        movements.Clear();
    }


}