Unity Mobile touch-screen operation

Source: Internet
Author: User

Unity Touch screen operation

When you run a unity game on an iOS or Android device, the left mouse button of the desktop system automatically changes to a touchscreen operation on the phone screen, but actions such as multi-touch screens are not available for mouse operation. Unity's input class contains not only the various input functions of the desktop system, but also the various functions for touch screen operation of mobile devices, and the use of the input class in touch operation is described below.

First introduce the input.touches structure, which is a touch array, each of which represents the touch state of the finger on the screen. Each finger touch is described by input.touches:

Fingerid

Unique index of touch

Position

Touch the location of the screen

Deltatime

The elapsed time from the last state to the current state

Tapcount

Number of clicks. Andorid device does not click Count, this method always returns 1

Deltaposition

The screen position changed since the last frame

Phase

Phase, which is the state of the screen operation

where phase (state) has the following types:

Began

Finger just touch the screen

Moved

Finger moves on the screen

Stationary

Fingers touch the screen, but no movement since the last burst

Ended

Finger off the screen

Canceled

The system cancels touch tracking for reasons such as placing the device on the face or over 5 touch points at a time

The following is a piece of code to implement a mobile touch operation:

usingUnityengine;usingSystem.Collections; Public classAndroidtouch:monobehaviour {Private intIsforward;//mark the direction of the camera's movement//record the old position of two fingers    PrivateVector2 oposition1=NewVector2 (); PrivateVector2 oposition2=NewVector2 (); Vector2 M_screenpos=NewVector2 ();//record where your finger is touching//used to determine whether to enlarge    BOOLIsenlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) {//The function passes the position of the last touch two point and the position of the touch two points to calculate the user's gesture        floatLeng1 = Mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (OP1.Y-OP2.Y) * (OP1.Y-op2.y)); floatLeng2 = Mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (NP1.Y-NP2.Y) * (NP1.Y-np2.y)); if(Leng1 <leng2) {            //Zoom gesture            return true; }        Else        {            //Zoom Out gesture            return false; }    }    voidStart () {input.multitouchenabled=true;//turn on multi-point touch    }    voidUpdate () {if(Input.touchcount <=0)              return; if(Input.touchcount = =1)//Single Touch Mobile camera        {            if(input.touches[0].phase = =Touchphase.began) M_screenpos= input.touches[0].position;//record where your finger just touched.            if(input.touches[0].phase = = touchphase.moved)//Move your finger on the screen, move the camera{transform. Translate (NewVector3 (input.touches[0].deltaposition.x * Time.deltatime, input.touches[0].DELTAPOSITION.Y * Time.deltatime,0)); }        }        Else if(Input.touchcount >1)//multi-touch.        {            //record the position of two fingersVector2 Nposition1 =NewVector2 (); Vector2 Nposition2=NewVector2 (); //record movement distance per frame of fingerVector2 deltaDis1 =NewVector2 (); Vector2 DeltaDis2=NewVector2 ();  for(inti =0; I <2; i++) {Touch Touch=Input.touches[i]; if(Touch.phase = =touchphase.ended) Break; if(Touch.phase = = touchphase.moved)//the fingers are moving .                {                    if(i = =0) {Nposition1=touch.position; DeltaDis1=touch.deltaposition; }                    Else{Nposition2=touch.position; DeltaDis2=touch.deltaposition; if(Isenlarge (Oposition1, Oposition2, Nposition1, Nposition2))//to determine the zoom effect of the camera before and after moving the signal .Isforward =1; ElseIsforward= -1; }                    //record the old touch locationOposition1 =Nposition1; Oposition2=Nposition2; }                //Mobile CameraCamera.main.transform.Translate (Isforward*vector3.forward * time.deltatime* (Mathf.abs (deltadis2.x+deltadis1.x) + Mathf.abs (deltadis1.y+( deltadis2.y))); }          }    }}

Bind the script to the main camera and discover that a single touch operation can move the camera up or down, and the dual touch operation scales.

Export Android to run on the phone, you can see the effect of touch.

Unity Mobile touch-screen operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.