Recommended for learning unity scripts: index on the unity3d Official Website
We recommend that you use the fingergestures plug-in for mouse gestures :.
You can also write scripts by yourself.
The source code of a simple demo script is as follows:
// Bind the reference object var target: transform; // The scaling factor var distance = 10.0; // The left and right sliding speed var xspeed = 250.0; var yspeed = 120.0; // zoom limit coefficient var yminlimit =-20; var ymaxlimit = 80; // The camera position var x = 0.0; var y = 0.0; // record the last touch position of the mobile phone to determine whether the user is zoomed in or out on the left. Private var oldposition1: vector2; private var oldposition2: vector2; // initialize the Game Information and set function start () {var angles = transform. eulerangles; X = angles. y; y = angles. x; // make the rigid body not change rotation if (Rigidbody) Rigidbody. freezerotation = true;} function Update () {// determines the number of touch points as single-touch if (input. touchcount = 1) {// The touch type is mobile touch if (input. gettouch (0 ). phase = touchphase. moved) {// calculate the X and Y positions x + = input based on the touch point. getaxis ("Mouse X") * xspeed * 0.02; y-= input. getaxis ("Mouse y") * yspeed * 0.02;} // determines the number of touch points. If (input. touchcount> 1) {// The first two finger touch types are all moving touch if (input. gettouch (0 ). phase = touchphase. moved | input. gettouch (1 ). phase = touchphase. moved) {// calculate the position of the current two-point touch point var tempposition1 = input. gettouch (0 ). position; var tempposition2 = input. gettouch (1 ). position; // if the function returns a scaled-in value, the false value is returned. If (isenlarge (oldposition1, oldposition2, tempposition1, tempposition2 )) {// After the amplification factor is greater than 3, you cannot zoom in. // The data here is adjusted based on the model in my project. You can modify if (distance> 3) by yourself) {distance-= 0.5 ;}} else {// The data here is adjusted according to the model in my project, you can modify if (distance <18.5) {distance + = 0.5 ;}/// back up the last touch point location, which is used to compare oldposition1 = tempposition1; oldposition2 = tempposition2 ;}}// if the function returns a zoom-in value, the false value is the zoom-out function isenlarge (OP1: vector2, OP2: vector2, Np1: vector2, NP2: vector2 ): boolean {// The function transmits the position of the last two points of touch and the position of the two points of this touch to calculate the user's gesture var leng1 = mathf. SQRT (op1.x-op2.x) * (op1.x-op2.x) + (op1.y-op2.y) * (op1.y-op2.y); var leng2 = mathf. SQRT (np1.x-np2.x) * (np1.x-np2.x) + (np1.y-np2.y) * (np1.y-np2.y); If (leng1 <leng2) {// zoom-In gesture return true;} else {// zoom-out gesture return false;} // The update method enters here after the call ends and calculates the position of the camera reset function lateupdate () {// target is the box variable we bind, and if (target) for scaling and rotating the reference object {// reset the camera position y = clampangle (Y, yminlimit, ymaxlimit ); vaR rotation = Quaternion. euler (Y, X, 0); var position = rotation * vector3 (0.0, 0.0,-distance) + target. position; transform. rotation = rotation; transform. position = position ;}} static function clampangle (angle: float, Min: float, Max: Float) {If (angle <-360) angle ++ = 360; if (angle> 360) angle-= 360; return mathf. clamp (angle, Min, max );}
You can bind it to the maincamera of the project and specify the scaled object:
Package the project into an APK and run it to view the scaling and Rotation effects.