A set of self-written for unity Mobile gesture operation of the judgment, mainly a single-finger moving 3D objects, single-finger rotation 3D objects, two-finger zoom 3D objects, here first separate two-finger zoom 3D object, as shown below:
Using Unityengine;
Using System.Collections;
public class Zoomcontrol:gesturecontrol {//Record previous phone touch location to determine whether the user is in the left or zoom out gesture private Vector2 oldPosition1;
Private Vector2 OldPosition2;
Real-time size Vector3 Realscale = new Vector3 (1f, 1f, 1f);
Original size float Initialscale = 0;
Zoom speed public float scalespeed = 0.1f;
Scale public float Maxscale = 2.5f;
public float Minscale = 0.5f;
void Start () {//Get object most primitive size Initialscale = this.transform.localscale.x; } protected override void Inputcheck () {#region Multi-touch scaling (true model scaling) if (input.touchcount ;
1) {status = 2;
Startcoroutine (Customonmousedown ()); } #endregion} IEnumerator Customonmousedown () {//When a constant touch is detected, the while (Input.get
Mousebutton (0)) {//real-time record model size Realscale = This.transform.localScale; if (Input.gettouch (0). Phase = = touchphase.moved | | Input.gettouch (1). Phase = = touchphase.moved) {//Touch position Vector3 TempPosition1 =
Input.gettouch (0). Position;
Vector3 TempPosition2 = Input.gettouch (1). Position;
function returns true for magnification, return false for narrowing if (Isenlarge (OldPosition1, OldPosition2, TempPosition1, TempPosition2))
{//Determine if the boundary is exceeded if (realscale.x < Initialscale * Maxscale) {
This.transform.localScale + = This.transform.localScale * scalespeed; }} else {//To determine whether to exceed the boundary if (realscal e.x > Initialscale * minscale) {This.transform.localScale-= This.transfor
M.localscale * SCALESPEED;
}}//Backup last touch position oldPosition1 = TempPosition1; OldPosition2 = TempPosition2;
} yield return new waitforfixedupdate (); }}//record finger position with initial position is reduced or enlarged bool Isenlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) {Floa
T leng1 = mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (OP1.Y-OP2.Y) * (OP1.Y-OP2.Y));
float leng2 = mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (NP1.Y-NP2.Y) * (NP1.Y-NP2.Y));
if (Leng1 < leng2) {return true;
} else {return false;
}
}
}