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.right) //判断当前方向  
                   {  
                       return;  
                   }                    
                   Debug.Log("right");  
                   currentVector = slideVector.right; //设置方向  
               }  
               else if (y > x && y < -x)  
               {  
                   if (currentVector == slideVector.left)  
                   {  
                       return;  
                   }  
                   Debug.Log("left");                    
                   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;       //初始化方向    
       }  
   }  


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


我个人默认距离是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;  
        }  
    }   // 滑动方法  
}  

阅读更多
个人分类: MySQL
上一篇Unity 3D:PC端(电脑)控制相机旋转、移动、缩放等功能
下一篇Unity判断手势触摸的类型 ,判断手势的滑动方向,并获取刚触摸以及触摸结束事的坐标
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭