效果如图:
上代码:
单指移动 OneFingerMove.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class OneFingerMove : MonoBehaviour { RectTransform rectTransform; // Start is called before the first frame update void Start() { rectTransform = transform as RectTransform; } // Update is called once per frame void Update() { #if UNITY_EDITOR || UNITY_STANDALONE Move_PC(); #else Move(); #endif } private float moveCo = 3f; //移动系数 private Vector2 endValue = Vector2.zero; private void Move() { if (Input.touchCount != 1) { return; } Touch touch1 = Input.GetTouch(0); if (touch1.phase == TouchPhase.Moved) { endValue = rectTransform.anchoredPosition + touch1.deltaPosition * moveCo; float clampX = Mathf.Clamp(endValue.x, -rectTransform.sizeDelta.x * transform.localScale.x / 2, rectTransform.sizeDelta.x * transform.localScale.x / 2); float clampY = Mathf.Clamp(endValue.y, -rectTransform.sizeDelta.y * transform.localScale.y / 2, rectTransform.sizeDelta.y * transform.localScale.y / 2); endValue = new Vector2(clampX, clampY); rectTransform.DOAnchorPos(endValue, 0.3f); } } private Vector3 oldMousePos; private void Move_PC() { if (Input.GetMouseButtonDown(0)) { oldMousePos = Input.mousePosition; } if (Input.GetMouseButton(0)) { Vector3 deltaPosition = Input.mousePosition - oldMousePos; endValue = rectTransform.anchoredPosition + new Vector2(deltaPosition.x, deltaPosition.y) * moveCo; float clampX = Mathf.Clamp(endValue.x, -rectTransform.sizeDelta.x * transform.localScale.x / 2, rectTransform.sizeDelta.x * transform.localScale.x / 2); float clampY = Mathf.Clamp(endValue.y, -rectTransform.sizeDelta.y * transform.localScale.y / 2, rectTransform.sizeDelta.y * transform.localScale.y / 2); endValue = new Vector2(clampX, clampY); rectTransform.DOAnchorPos(endValue, 0.3f); oldMousePos = Input.mousePosition; } } public void Reset() { endValue = Vector2.zero; rectTransform.DOAnchorPos(endValue, 0.3f); } } |
双指缩放 TwoFingerZoom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class TwoFingerZoom : MonoBehaviour { RectTransform rectTransform; // Start is called before the first frame update void Start() { rectTransform = transform as RectTransform; } // Update is called once per frame void Update() { #if UNITY_EDITOR || UNITY_STANDALONE Zoom_PC(); #else Zoom(); #endif } private float currentZoomScale = 1; private float zoomScaleMax = 2; private float zoomScaleMin = 0.3f; private float zoomScaleCo = 0.003f; //缩放系数 Touch oldTouch1; //上次触摸点1(手指1) Touch oldTouch2; //上次触摸点2(手指2) private void Zoom() { if (Input.touchCount <= 1) { return; } Touch touch1 = Input.GetTouch(0); Touch touch2 = Input.GetTouch(1); //启用双指,尚未旋转 if (touch2.phase == TouchPhase.Began) { oldTouch2 = touch2; oldTouch1 = touch1; return; } if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) { float oldDistance = Vector2.Distance(oldTouch2.position, oldTouch1.position); float curDistance = Vector2.Distance(touch2.position, touch1.position); currentZoomScale += (curDistance - oldDistance) * zoomScaleCo; currentZoomScale = Mathf.Clamp(currentZoomScale, zoomScaleMin, zoomScaleMax); transform.DOScale(Vector3.one * currentZoomScale, 0.3f); float clampX = Mathf.Clamp(rectTransform.anchoredPosition.x, -rectTransform.sizeDelta.x * transform.localScale.x / 2, rectTransform.sizeDelta.x * transform.localScale.x / 2); float clampY = Mathf.Clamp(rectTransform.anchoredPosition.y, -rectTransform.sizeDelta.y * transform.localScale.y / 2, rectTransform.sizeDelta.y * transform.localScale.y / 2); Vector2 endValue = new Vector2(clampX, clampY); rectTransform.DOAnchorPos(endValue, 0.3f); oldTouch1 = touch1; oldTouch2 = touch2; } } private void Zoom_PC() { if (Input.GetAxis("Mouse ScrollWheel") < 0) { currentZoomScale -= 0.3f; currentZoomScale = Mathf.Clamp(currentZoomScale, zoomScaleMin, zoomScaleMax); transform.DOScale(Vector3.one * currentZoomScale, 0.3f); float clampX = Mathf.Clamp(rectTransform.anchoredPosition.x, -rectTransform.sizeDelta.x * transform.localScale.x / 2, rectTransform.sizeDelta.x * transform.localScale.x / 2); float clampY = Mathf.Clamp(rectTransform.anchoredPosition.y, -rectTransform.sizeDelta.y * transform.localScale.y / 2, rectTransform.sizeDelta.y * transform.localScale.y / 2); Vector2 endValue = new Vector2(clampX, clampY); rectTransform.DOAnchorPos(endValue, 0.3f); } if (Input.GetAxis("Mouse ScrollWheel") > 0) { currentZoomScale += 0.3f; currentZoomScale = Mathf.Clamp(currentZoomScale, zoomScaleMin, zoomScaleMax); transform.DOScale(Vector3.one * currentZoomScale, 0.3f); float clampX = Mathf.Clamp(rectTransform.anchoredPosition.x, -rectTransform.sizeDelta.x * transform.localScale.x / 2, rectTransform.sizeDelta.x * transform.localScale.x / 2); float clampY = Mathf.Clamp(rectTransform.anchoredPosition.y, -rectTransform.sizeDelta.y * transform.localScale.y / 2, rectTransform.sizeDelta.y * transform.localScale.y / 2); Vector2 endValue = new Vector2(clampX, clampY); rectTransform.DOAnchorPos(endValue, 0.3f); } } public void Reset() { currentZoomScale = 1; transform.DOScale(Vector3.one * currentZoomScale, 0.3f); } } |
双指旋转 TwoFingerRotate.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TwoFingerRotate : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Rotate(); } private float rotateCo = 1f; //旋转系数 Touch oldTouch1; //上次触摸点1(手指1) Touch oldTouch2; //上次触摸点2(手指2) private void Rotate() { if (Input.touchCount <= 1) { return; } Touch touch1 = Input.GetTouch(0); Touch touch2 = Input.GetTouch(1); //启用双指,尚未旋转 if (touch2.phase == TouchPhase.Began) { oldTouch1 = touch1; oldTouch2 = touch2; return; } if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) { Vector2 curVec = touch2.position - touch1.position; Vector2 oldVec = oldTouch2.position - oldTouch1.position; float angle = Vector2.Angle(oldVec, curVec); angle *= Mathf.Sign(Vector3.Cross(oldVec, curVec).z); transform.Rotate(new Vector3(0, 0, angle) * rotateCo); oldTouch1 = touch1; oldTouch2 = touch2; } } public void Reset() { transform.localEulerAngles = Vector3.zero; } } |
- 本文固定链接: http://www.u3d8.com/?p=2213
- 转载请注明: 网虫虫 在 u3d8.com 发表过