using UnityEngine;
using System.Collections;
public class TouchMoveTest : MonoBehaviour {
private Touch oldTouch1; //上次触摸点1(手指1)
private Touch oldTouch2; //上次触摸点2(手指2)
private float offset;
private float scaleFactor;
private Vector3 localScale;
private Vector3 scale;
void Start()
{
}
void Update()
{
//没有触摸
if (Input.touchCount <= 0)
{
return;
}
//单点触摸, 水平上下旋转
if (Input.touchCount==1)
{
Touch touch = Input.GetTouch(0);
Vector2 deltaPos = touch.deltaPosition;
transform.Rotate(Vector3.down * deltaPos.x, Space.World);
transform.Rotate(Vector3.right * deltaPos.y, Space.World);
}
//多点触摸, 放大缩小
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2点刚开始接触屏幕, 只记录,不做处理
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离之差,为正表示放大手势, 为负表示缩小手势
offset = newDistance - oldDistance;
//放大因子, 一个像素按 0.01倍来算(100可调整)
scaleFactor = offset / 100f;
scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
//最大放大到3.0倍
if(scale.x<3.0f && scale.y<3.0f && scale.z < 3.0f)
{
localScale = transform.localScale;
}
//最小缩放到 0.3 倍
if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f)
{
transform.localScale = scale;
}
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
void OnGUI()
{
GUI.Label(new Rect(100, 50, 200, 20), "offset is " + offset);
GUI.Label(new Rect(100, 100, 200, 20), "scaleFactor is " + scaleFactor);
GUI.Label(new Rect(100, 150, 400, 20), "localScale.x is " + localScale.x);
}
}