Unity 3D control script for touch on mobile phone screen

Unity determines the type of gesture touch, determines the sliding direction of the gesture, and obtains the coordinates of the touch and the end of the touch

Single touch
Input.touchCount1
Mobile touch
Input.GetTouch(0).phase
TouchPhase.Moved
Multi-touch
Input.touchCount> 1
Determines that at least one of the two fingers is a mobile touch
Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved

/**
* Determine whether it is a single touch
**/
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;

}

/**
*
* Create a new public method for judging the direction of finger movement
* If it is left or up, the model moves to the positive direction of each axis. The function returns 1
* Add is right or down, the model moves to each axis The negative direction position movement function returns -1
*
* **/
int judueFinger(){ if (Input.GetTouch(0).phase == TouchPhase.Began && startPosFlag == true) { //Debug.Log("==


Start touching=");
startFingerPos = Input.GetTouch(0).position;
startPosFlag = false;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
//Debug.Log("==Release touch=");
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("===Move along the negative direction of the X axis=");
backValue = -1; //Move along the negative direction of the X axis
}
else
{ //Debug.Log("===
Move along the positive X axis=");
backValue = 1; //Move along the positive direction of the X axis
}
}
else
{ if (nowFingerPos.y-startFingerPos.y>0) { //Debug.Log("===


Move along the positive direction of the Y axis=");
backValue = 1; //Move along the positive direction of the Y axis
}else{ //Debug.Log("===
Move along the negative direction of the Y axis=");
backValue = -1; //Move along the negative direction of Y axis
}
}
return backValue;
}

Guess you like

Origin blog.csdn.net/jiachun199/article/details/107658715