First, some Input.touches structure, which is a touch array, each record represents the state of the finger on the touch screen. Each finger touch is through Input.touches to describe:
fingerId |
The only index touched |
position |
Position of the touch screen |
participate hour |
From the final state to the current state of the elapsed time |
tapCount |
Hits. Click on Andorid device does not count, this method always returns 1 |
delta position |
The change from the last position of a screen |
phase |
Phase, i.e. the screen operating state |
Which phase (state) There are these types:
Began |
Just a finger touches the screen |
Moved |
Move your finger on the screen |
Stationary |
Finger touches the screen, but did not move for a while since the last |
Ended |
Your finger off the screen |
Canceled |
Tracking system deactivates the touch, the reason for such equipment on the face or over five touch points simultaneously
|
Unity in the game is running the left mouse button operation can be replaced with a touch screen on the phone, but can not replace the multi-point touch. (That is to say on the phone when you call getaxis and getmousebutton methods such as touch screens can also respond)
The following is achieved rotation and zoom code by operating on the phone
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public float rotateSpeed; public Vector2 nTouchPos1; public Vector2 nTouchPos2; public Vector2 oTouchPos1; public Vector2 oTouchPos2; // Update is called once per frame void Update() { // rotation if (Input.GetMouseButton(0)) { if (Input.touchCount == 1) { if (Input.GetTouch(0).phase == TouchPhase.Moved) { Debug.Log ( "start rotating"); float moveX = Input.GetTouch(0).deltaPosition.x; Debug.Log ( "sliding distance" + moveX); transform.Rotate(Vector3.up * -rotateSpeed * moveX * Time.deltaTime); } } } ZoomInOut(); if (this.transform.position.y > 0) { return; } this.transform.Translate(new Vector3(0, 0.5f, 0) * Time.deltaTime); } bool IsEnLarge(Vector2 nPos1,Vector2 nPos2,Vector2 oPos1,Vector2 oPos2) { float nDis = Vector2.Distance(nPos1, nPos2); float oDis = Vector2.Distance(oPos1, oPos2); if(nDis<oDis) { return false; } else { return true; } } void ZoomInOut() { if(Input.touchCount==2) { if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved) { nTouchPos1 = Input.GetTouch(0).position; nTouchPos2 = Input.GetTouch(1).position; if(IsEnLarge(nTouchPos1,nTouchPos2,oTouchPos1,oTouchPos2)) { Vector3 nScale = transform.localScale*1.01f; transform.localScale = new Vector3(nScale.x, nScale.y, nScale.z); } else { Vector3 nScale = transform.localScale *0.99f; transform.localScale = new Vector3(nScale.x, nScale.y, nScale.z); } oTouchPos1 = nTouchPos1; oTouchPos2 = nTouchPos2; } } } }