Unity 3D 对手机屏幕触摸的控制脚本

Unity判断手势触摸的类型 ,判断手势的滑动方向,并获取刚触摸以及触摸结束事的坐标

单点触摸
Input.touchCount1
移动触摸
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;
}

猜你喜欢

转载自blog.csdn.net/jiachun199/article/details/107658715
今日推荐