unity 简单手势判断(一)

来源:互联网 发布:淘宝模板页头 编辑:程序博客网 时间:2024/05/17 01:48

在移动设备上经常会有手势操作:方向滑动判断,缩放,乃至稍微复杂的一些,画圈,S移动等;当然目前有很多手势插件可以用,不过能自己弄出来还是不错的,这里先上一个简单的方向滑动判断

--------------------------------------------------------------------我是分割线---------------------------------------------------------------------------------------------------------


 #region---------------------手势判断----------------------    enum FingerTouchType    {        type_LeftMove,        type_RightMove,        type_UpMove,        type_DownMove    }    //记录触屏位置    Vector2 screenPos = new Vector2();    /// <summary>    /// 判断函数,放在update里执行    /// </summary>    void FingerTouch()    {        if (Input.touchCount <= 0)            return;        if (Input.touchCount == 1)        {            if (Input.touches[0].phase == TouchPhase.Began)            {                //记录位置                screenPos = Input.touches[0].position;            }            else if (Input.touches[0].phase == TouchPhase.Moved)            {            }            if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)            {                Vector2 pos = Input.touches[0].position;
                //这里可以处理自己的事件                switch (CheckTouch(screenPos, pos))                {                    case FingerTouchType.type_LeftMove:                        {                                                        break;                        }                    case FingerTouchType.type_RightMove:                        {                                                        break;                        }                    case FingerTouchType.type_UpMove:                        {                                                        break;                        }                    case FingerTouchType.type_DownMove:                        {                                                        break;                        }                }            }        }        else if (Input.touchCount > 1)        {        }    }    FingerTouchType CheckTouch(Vector2 start, Vector2 end)    {        if (Mathf.Abs(start.x - end.x) > Mathf.Abs(start.y - end.y))        {            if (start.x > end.x)            {                //左滑                return FingerTouchType.type_LeftMove;            }            else            {                //右滑                return FingerTouchType.type_RightMove;            }        }        else//垂直滑动        {            if (start.y > end.y)            {                //下滑                return FingerTouchType.type_DownMove;            }            else            {                //上滑                return FingerTouchType.type_UpMove;            }        }    }    #endregion

原创粉丝点击