Unity3d 触屏手机滑动事件(上下左右方向精确判断)

 private Vector2 st = Vector2.zero; //手指开始按下的位置  
    private Vector2 end = Vector2.zero; //手指拖动的位置  
    enum slideVector { nullVector,up,down, left, right }; //上下左右四个方向
    private slideVector currentVector = slideVector.nullVector; //默认是null
    private float timer;//时间计数器  
    public float offsetTime = 0.01f;//判断的时间间隔  

    void OnGUI()
    {
        if (Event.current.type == EventType.MouseDown)//判断当前手指是按下事件  
        {
            st = Event.current.mousePosition;//记录开始按下的位置  
        }
        if (Event.current.type == EventType.MouseDrag)//判断当前手指是拖动事件  

        {
            timer += Time.deltaTime;  //计时器

            if (timer > offsetTime)
            {
                end = Event.current.mousePosition; //记录结束下的位置
                Vector2 slideDirection = st - end;
                float x = slideDirection.x;
                float y = slideDirection.y;

                if (y < x && y > -x)
                {
                    if (currentVector == SlideVector.left) //判断当前方向
                    {
                        return;
                    }                  
                    Debug.Log("left");
                    currentVector = SlideVector.left; //设置方向
                }
                else if (y > x && y < -x)
                {
                    if (currentVector == SlideVector.right)
                    {
                        return;
                    }
                    Debug.Log("right");                  
                    currentVector = SlideVector.right;
                }
                else if (y > x && y > -x)
                {
                    if (currentVector == SlideVector.up)
                    {
                        return;
                    }
                    Debug.Log("up");               
                    currentVector = SlideVector.up;
                }
                else if (y < x && y < -x)
                {
                    if (currentVector == SlideVector.down)
                    {
                        return;
                    }
                    Debug.Log("down");                 
                    currentVector = SlideVector.down;
                }
                timer = 0;
                st = end;//初始化位置
               
            }        
        }
        if (Event.current.type == EventType.MouseUp)
        {
            currentVector = slideVector.nullVector;       //初始化方向  
        }
    }

 

2017-07-05 更新   

添加了一个滑动距离 不然手指刚刚触碰上去就开始判断了滑动  

我个人默认距离是80  滑动距离超过80的时候才开始执行方法

 

    enum slideVector { nullVector, up, down, left, right };
    private Vector2 touchFirst = Vector2.zero; //手指开始按下的位置
    private Vector2 touchSecond = Vector2.zero; //手指拖动的位置
    private slideVector currentVector = slideVector.nullVector;//当前滑动方向
    private float timer;//时间计数器  
    public float offsetTime = 0.1f;//判断的时间间隔 
    public float SlidingDistance = 80f;

    void OnGUI()   // 滑动方法02
    {
        if (Event.current.type == EventType.MouseDown)
        //判断当前手指是按下事件 
        {
            touchFirst = Event.current.mousePosition;//记录开始按下的位置
        }
        if (Event.current.type == EventType.MouseDrag)
        //判断当前手指是拖动事件
        {
            touchSecond = Event.current.mousePosition;

            timer += Time.deltaTime;  //计时器

            if (timer > offsetTime)
            {
                touchSecond = Event.current.mousePosition; //记录结束下的位置
                Vector2 slideDirection = touchFirst - touchSecond;
                float x = slideDirection.x;
                float y = slideDirection.y;

                if (y + SlidingDistance < x && y > -x - SlidingDistance)
                {

                    if (currentVector == slideVector.left)
                    {
                        return;
                    }

                    Debug.Log("right");
                 
                    currentVector = slideVector.left;
                }
                else if (y > x + SlidingDistance && y < -x - SlidingDistance)
                {
                    if (currentVector == slideVector.right)
                    {
                        return;
                    }

                    Debug.Log("left");
       
                    currentVector = slideVector.right;
                }
                else if (y > x + SlidingDistance && y - SlidingDistance > -x)
                {
                    if (currentVector == slideVector.up)
                    {
                        return;
                    }

                    Debug.Log("up");
                  
                    currentVector = slideVector.up;
                }
                else if (y + SlidingDistance < x && y < -x - SlidingDistance)
                {
                    if (currentVector == slideVector.down)
                    {
                        return;
                    }

                    Debug.Log("Down");
                   
                    currentVector = slideVector.down;
                }

                timer = 0;
                touchFirst = touchSecond;
            }
            if (Event.current.type == EventType.MouseUp)
            {//滑动结束  
                currentVector = slideVector.nullVector;
            }
        }   // 滑动方法
    }  

 

 

 

 

 

你可能感兴趣的:(Unity3d 触屏手机滑动事件(上下左右方向精确判断))