Unity判断手势触摸的类型 判断手势的滑动方向,控制物体移动


本章咱们一起来看下unity对有触摸手势做出的响应
单点触摸
Input.touchCount==1
移动触摸
Input.GetTouch(0).phase==TouchPhase.Moved
多点触摸
Input.touchCount > 1
判断两只手指至少有一只为移动触摸
Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved

/**
     * 判断是否为单点触摸
     **/
    public static bool singleTouch()
    {
        if(Input.touchCount==1)
            return true;
        return false;
    }

    /**
     * 判断单点触摸条件下  是否为移动触摸
     **/
    public static bool moveSingleTouch()
    {
        if (Input.GetTouch(0).phase==TouchPhase.Moved)
            return true;
        return false;
    }

    /**
     *判断是否为多点触摸
     **/
    public static bool multipointTouch()
    {
        if (Input.touchCount > 1)
            return true;
        return false;
    }

    /**
     *判断两只手指至少有一只为移动触摸
     **/
    public static bool moveMultiTouch()
    {
        if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            return true;
        return false;
}

/**
     *
     * 新建一个公共方法用于判断手指的移动方向
     * 假如是往左或者往上 则模型往各个轴的正方向位置移动 函数返回1
     * 加入是往右或者往下 则模型往各个轴的负方向位置移动 函数返回-1
     *
     * **/
    int judueFinger(){
        if (Input.GetTouch(0).phase == TouchPhase.Began && startPosFlag == true)
        {
            //Debug.Log("======开始触摸=====");
            startFingerPos = Input.GetTouch(0).position;
            startPosFlag = false;
        }
        if (Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            //Debug.Log("======释放触摸=====");
            startPosFlag = true;
        }
        nowFingerPos = Input.GetTouch(0).position;
        xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x);
        yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y);
        if (xMoveDistance>yMoveDistance)
        {
            if(nowFingerPos.x-startFingerPos.x>0){
                //Debug.Log("=======沿着X轴负方向移动=====");
                backValue = -1;         //沿着X轴负方向移动
            }
            else
            {
                //Debug.Log("=======沿着X轴正方向移动=====");
                backValue = 1;          //沿着X轴正方向移动
            }
        }
        else
        {
            if (nowFingerPos.y - startFingerPos.y>0)
            {
                //Debug.Log("=======沿着Y轴正方向移动=====");
                backValue = 1;         //沿着Y轴正方向移动
            }else{
                //Debug.Log("=======沿着Y轴负方向移动=====");
                backValue = -1;         //沿着Y轴负方向移动
            }
        }
        return backValue;
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM