using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class AndroidTouch : MonoBehaviour { private Vector3 DefaultPostion; private float addValue; private float scale; private const float maxDis = 6; private Touch oldTouch1; //上次触摸点1(手指1) private Touch oldTouch2; //上次触摸点2(手指2) private void Start() { DefaultPostion = this.transform.position; } void Update() { //没有触摸,就是触摸点为0 if (Input.touchCount <= 0) { return; } //单点触摸, 水平上下旋转 if (1 == Input.touchCount) { Touch touch = Input.GetTouch(0); Vector2 deltaPos = touch.deltaPosition/ 2; if (touch.position.x < Screen.width / 2) { transform.Rotate(Vector3.down * deltaPos.x, Space.Self);//绕Y轴进行旋转 } else { transform.Rotate(Vector3.right * deltaPos.y, Space.Self);//绕X轴进行旋转,下面我们还可以写绕Z轴进行旋转 } return; } //多点触摸, 放大缩小 Touch newTouch1 = Input.GetTouch(0); Touch newTouch2 = Input.GetTouch(1); //第2点刚开始接触屏幕, 只记录,不做处理 if (newTouch2.phase == TouchPhase.Began) { oldTouch2 = newTouch2; oldTouch1 = newTouch1; //addValue = 0; return; } //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型 float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position); float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position); //两个距离之差,为正表示放大手势, 为负表示缩小手势 float offset = newDistance - oldDistance; //放大因子, 一个像素按 0.01倍来算(100可调整) float scaleFactor = - offset / 100f; addValue += scaleFactor; Vector3 localScale = transform.localScale; Vector3 scale = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor); //在什么情况下进行缩放 if (addValue < maxDis && addValue >0) { this.transform.position = DefaultPostion + Vector3.forward * addValue; } else { CDebug.Log(scale.ToString() + "-->" + addValue); } if (addValue > maxDis) { addValue = maxDis; } else if (addValue < 0) { addValue = 0; } //记住最新的触摸点,下次使用 oldTouch1 = newTouch1; oldTouch2 = newTouch2; } public void RefreshPos() { CDebug.Log("RefreshPos"); this.transform.position = DefaultPostion; //this.transform.eulerAngles = FollowTarget.eulerAngles.y * Vector3.up; this.transform.position +=Vector3.forward * scale; //this.transform.localScale = this.transform.localScale * scale; } }