多点触控(unity)
using UnityEngine;
using System.Collections;
public class TouchTest : MonoBehaviour
{
public GameObject ball;
private float lastDis = 0;
private float cameraDis = -20;
public float ScaleDump = 0.1f;
void Update()
{
if (Input.touchCount == 1)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Moved)
{
ball.transform.Rotate(Vector3.right, Input.GetAxis("Mouse Y"), Space.World);
ball.transform.Rotate(Vector3.up, -1 * Input.GetAxis("Mouse X"), Space.World);
}
}
else if (Input.touchCount > 1)
{
Touch t1 = Input.GetTouch(0);
Touch t2 = Input.GetTouch(1);
if (t2.phase == TouchPhase.Began)
{
lastDis = Vector2.Distance(t1.position, t2.position);
}
else
if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
{
float dis = Vector2.Distance(t1.position, t2.position);
if (Mathf.Abs(dis - lastDis) > 1)
{
cameraDis += (dis - lastDis) * ScaleDump;
cameraDis = Mathf.Clamp(cameraDis, -40, -5);
lastDis = dis;
}
}
}
}
private void LateUpdate()
{
this.transform.position = new Vector3(0, 0, cameraDis);
}
private void OnGUI()
{
string s = string.Format("Input.touchCount={0}\ncameraDis=\n{1}",
Input.touchCount, cameraDis);
GUI.TextArea(new Rect(0, 0, Screen.width / 10, Screen.height), s);
if (GUI.Button(new Rect(Screen.width*9/10,0,Screen.width/10,Screen.height/10),"quit"))
{
Debug.Log("quit");
Application.Quit();
}
}
}