using UnityEngine; using System.Collections; /// /// 触摸 /// public class OrbitTouch : MonoBehaviour { /// /// you need this for mobile only, so if you use it for mobile set as false /// [Header("Movement")] [SerializeField] private CameraOrbit m_CameraOrbit; [SerializeField] private Vector2 MovementMultiplier = new Vector2(1, 1); [Header("Pinch Zoom")] public bool CancelRotateOnPinch = true; [SerializeField, Range(0.01f, 2)] private float m_PinchZoomSpeed = 0.5f; private Vector2 direction; private Vector2 smoothDirection; private bool touched; private int pointerID; private bool Pinched = false; void Awake() { m_CameraOrbit = GetComponent(); direction = Vector2.zero; touched = false; } /// /// /// void Update() { ControlInput(); } void ControlInput() { for (int i = 0; i < Input.touchCount; i++) { Touch t = Input.touches[i]; if (t.phase == TouchPhase.Began) { OnPointerDown(t); } else if (t.phase == TouchPhase.Moved) { OnDrag(t); } else if (t.phase == TouchPhase.Ended) { OnPointerUp(t); } } } /// /// /// /// public void OnPointerDown(Touch data) { if (m_CameraOrbit == null) { Debug.LogWarning("Please assign a camera orbit target"); return; } if (!touched) { touched = true; pointerID = data.fingerId; } } /// /// /// /// public void OnDrag(Touch data) { if (m_CameraOrbit == null) { Debug.LogWarning("Please assign a camera orbit target"); return; } PinchZoom(); if (Pinched) return; if (data.fingerId == pointerID) { direction = data.deltaPosition.normalized; m_CameraOrbit.Horizontal = (direction.x * MovementMultiplier.x); m_CameraOrbit.Vertical = (-direction.y * MovementMultiplier.y); } } /// /// /// void ReanudeControl() { m_CameraOrbit.Controlable = true; m_CameraOrbit.CanRotate = true; Pinched = false; } /// /// /// void PinchZoom() { // If there are two touches on the device... if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) { // Store both touches. Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); // Find the position in the previous frame of each touch. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; // Otherwise change the field of view based on the change in distance between the touches. m_CameraOrbit.SetStaticZoom(deltaMagnitudeDiff * (m_PinchZoomSpeed / Mathf.PI)); if (CancelRotateOnPinch) { CancelInvoke("ReanudeControl"); m_CameraOrbit.Controlable = false; m_CameraOrbit.CanRotate = false; Invoke("ReanudeControl", 0.2f); Pinched = true; } } } /// /// /// /// public void OnPointerUp(Touch data) { if (m_CameraOrbit == null) { Debug.LogWarning("Please assign a camera orbit target"); return; } if (data.fingerId == pointerID) { direction = Vector2.zero; touched = false; } } }