{"id":2213,"date":"2021-01-07T15:44:25","date_gmt":"2021-01-07T07:44:25","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=2213"},"modified":"2021-01-07T15:44:27","modified_gmt":"2021-01-07T07:44:27","slug":"unity%e7%a7%bb%e5%8a%a8%e3%80%81%e6%97%8b%e8%bd%ac%e3%80%81%e7%bc%a9%e6%94%be%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=2213","title":{"rendered":"Unity\u79fb\u52a8\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u64cd\u4f5c"},"content":{"rendered":"\n<p>\u6548\u679c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/01\/fingerOperation.gif\" data-lightbox=\"image_lg\"><img title=\"Unity\u79fb\u52a8\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u64cd\u4f5c - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u79fb\u52a8\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u64cd\u4f5c - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" width=\"495\" height=\"282\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/01\/fingerOperation.gif\" alt=\"\" class=\"wp-image-2214\"\/><\/a><\/figure>\n\n\n\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>\u5355\u6307\u79fb\u52a8 OneFingerMove.cs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing DG.Tweening;\r\n\r\npublic class OneFingerMove : MonoBehaviour\r\n{\r\n    RectTransform rectTransform;\r\n    \/\/ Start is called before the first frame update\r\n    void Start()\r\n    {\r\n        rectTransform = transform as RectTransform;\r\n    }\r\n\r\n    \/\/ Update is called once per frame\r\n    void Update()\r\n    {\r\n#if UNITY_EDITOR || UNITY_STANDALONE\r\n        Move_PC();\r\n#else\r\n        Move();\r\n#endif\r\n    }\r\n\r\n    private float moveCo = 3f;    \/\/\u79fb\u52a8\u7cfb\u6570\r\n    private Vector2 endValue = Vector2.zero;\r\n    private void Move()\r\n    {\r\n        if (Input.touchCount != 1)\r\n        {\r\n            return;\r\n        }\r\n\r\n        Touch touch1 = Input.GetTouch(0);\r\n        if (touch1.phase == TouchPhase.Moved)\r\n        {\r\n            endValue = rectTransform.anchoredPosition + touch1.deltaPosition * moveCo;\r\n            float clampX = Mathf.Clamp(endValue.x, -rectTransform.sizeDelta.x * transform.localScale.x \/ 2, rectTransform.sizeDelta.x * transform.localScale.x \/ 2);\r\n            float clampY = Mathf.Clamp(endValue.y, -rectTransform.sizeDelta.y * transform.localScale.y \/ 2, rectTransform.sizeDelta.y * transform.localScale.y \/ 2);\r\n            endValue = new Vector2(clampX, clampY);\r\n            rectTransform.DOAnchorPos(endValue, 0.3f);\r\n        }\r\n    }\r\n\r\n    private Vector3 oldMousePos;\r\n    private void Move_PC()\r\n    {\r\n        if (Input.GetMouseButtonDown(0))\r\n        {\r\n            oldMousePos = Input.mousePosition;\r\n        }\r\n\r\n        if (Input.GetMouseButton(0))\r\n        {\r\n            Vector3 deltaPosition = Input.mousePosition - oldMousePos;\r\n            endValue = rectTransform.anchoredPosition + new Vector2(deltaPosition.x, deltaPosition.y) * moveCo;\r\n            float clampX = Mathf.Clamp(endValue.x, -rectTransform.sizeDelta.x * transform.localScale.x \/ 2, rectTransform.sizeDelta.x * transform.localScale.x \/ 2);\r\n            float clampY = Mathf.Clamp(endValue.y, -rectTransform.sizeDelta.y * transform.localScale.y \/ 2, rectTransform.sizeDelta.y * transform.localScale.y \/ 2);\r\n            endValue = new Vector2(clampX, clampY);\r\n            rectTransform.DOAnchorPos(endValue, 0.3f);\r\n            \r\n            oldMousePos = Input.mousePosition;\r\n        }\r\n    }\r\n\r\n    public void Reset()\r\n    {\r\n        endValue = Vector2.zero;\r\n        rectTransform.DOAnchorPos(endValue, 0.3f);\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>\u53cc\u6307\u7f29\u653e TwoFingerZoom.cs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing DG.Tweening;\r\n\r\npublic class TwoFingerZoom : MonoBehaviour\r\n{\r\n    RectTransform rectTransform;\r\n    \/\/ Start is called before the first frame update\r\n    void Start()\r\n    {\r\n        rectTransform = transform as RectTransform;\r\n    }\r\n\r\n    \/\/ Update is called once per frame\r\n    void Update()\r\n    {\r\n#if UNITY_EDITOR || UNITY_STANDALONE\r\n        Zoom_PC();\r\n#else\r\n        Zoom();\r\n#endif\r\n    }\r\n\r\n    private float currentZoomScale = 1;\r\n    private float zoomScaleMax = 2;\r\n    private float zoomScaleMin = 0.3f;\r\n    private float zoomScaleCo = 0.003f;   \/\/\u7f29\u653e\u7cfb\u6570\r\n    Touch oldTouch1;  \/\/\u4e0a\u6b21\u89e6\u6478\u70b91(\u624b\u63071)\r\n\tTouch oldTouch2;  \/\/\u4e0a\u6b21\u89e6\u6478\u70b92(\u624b\u63072)\r\n    private void Zoom()\r\n    {\r\n\t\tif (Input.touchCount &lt;= 1) {\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tTouch touch1 = Input.GetTouch(0);\r\n\t\tTouch touch2 = Input.GetTouch(1);\r\n \r\n\t\t\/\/\u542f\u7528\u53cc\u6307\uff0c\u5c1a\u672a\u65cb\u8f6c\r\n\t\tif (touch2.phase == TouchPhase.Began) {\r\n\t\t\toldTouch2 = touch2;\r\n\t\t\toldTouch1 = touch1;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tif (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) {\r\n            float oldDistance = Vector2.Distance(oldTouch2.position, oldTouch1.position);\r\n            float curDistance = Vector2.Distance(touch2.position, touch1.position);\r\n            currentZoomScale += (curDistance - oldDistance) * zoomScaleCo;\r\n            currentZoomScale = Mathf.Clamp(currentZoomScale, zoomScaleMin, zoomScaleMax);\r\n            transform.DOScale(Vector3.one * currentZoomScale, 0.3f);\r\n \r\n            float clampX = Mathf.Clamp(rectTransform.anchoredPosition.x, -rectTransform.sizeDelta.x * transform.localScale.x \/ 2, rectTransform.sizeDelta.x * transform.localScale.x \/ 2);\r\n            float clampY = Mathf.Clamp(rectTransform.anchoredPosition.y, -rectTransform.sizeDelta.y * transform.localScale.y \/ 2, rectTransform.sizeDelta.y * transform.localScale.y \/ 2);\r\n            Vector2 endValue = new Vector2(clampX, clampY);\r\n            rectTransform.DOAnchorPos(endValue, 0.3f);\r\n\r\n\t\t\toldTouch1 = touch1;\r\n\t\t\toldTouch2 = touch2;\r\n\t\t}\r\n    }\r\n\r\n    private void Zoom_PC()\r\n    {\r\n        if (Input.GetAxis(\"Mouse ScrollWheel\") &lt; 0)\r\n        {\r\n            currentZoomScale -= 0.3f;\r\n            currentZoomScale = Mathf.Clamp(currentZoomScale, zoomScaleMin, zoomScaleMax);\r\n            transform.DOScale(Vector3.one * currentZoomScale, 0.3f);\r\n            \r\n            float clampX = Mathf.Clamp(rectTransform.anchoredPosition.x, -rectTransform.sizeDelta.x * transform.localScale.x \/ 2, rectTransform.sizeDelta.x * transform.localScale.x \/ 2);\r\n            float clampY = Mathf.Clamp(rectTransform.anchoredPosition.y, -rectTransform.sizeDelta.y * transform.localScale.y \/ 2, rectTransform.sizeDelta.y * transform.localScale.y \/ 2);\r\n            Vector2 endValue = new Vector2(clampX, clampY);\r\n            rectTransform.DOAnchorPos(endValue, 0.3f);\r\n        }\r\n\t    if (Input.GetAxis(\"Mouse ScrollWheel\") > 0)\r\n\t    {\r\n            currentZoomScale += 0.3f;\r\n            currentZoomScale = Mathf.Clamp(currentZoomScale, zoomScaleMin, zoomScaleMax);\r\n            transform.DOScale(Vector3.one * currentZoomScale, 0.3f);\r\n            \r\n            float clampX = Mathf.Clamp(rectTransform.anchoredPosition.x, -rectTransform.sizeDelta.x * transform.localScale.x \/ 2, rectTransform.sizeDelta.x * transform.localScale.x \/ 2);\r\n            float clampY = Mathf.Clamp(rectTransform.anchoredPosition.y, -rectTransform.sizeDelta.y * transform.localScale.y \/ 2, rectTransform.sizeDelta.y * transform.localScale.y \/ 2);\r\n            Vector2 endValue = new Vector2(clampX, clampY);\r\n            rectTransform.DOAnchorPos(endValue, 0.3f);\r\n\t    }\r\n\r\n    }\r\n    \r\n    public void Reset() {\r\n        currentZoomScale = 1;\r\n        transform.DOScale(Vector3.one * currentZoomScale, 0.3f);\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>\u53cc\u6307\u65cb\u8f6c TwoFingerRotate.cs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class TwoFingerRotate : MonoBehaviour\r\n{\r\n    \/\/ Start is called before the first frame update\r\n    void Start()\r\n    {\r\n        \r\n    }\r\n\r\n    \/\/ Update is called once per frame\r\n    void Update()\r\n    {\r\n        Rotate();\r\n    }\r\n    \r\n    private float rotateCo = 1f;    \/\/\u65cb\u8f6c\u7cfb\u6570\r\n    Touch oldTouch1;  \/\/\u4e0a\u6b21\u89e6\u6478\u70b91(\u624b\u63071)\r\n\tTouch oldTouch2;  \/\/\u4e0a\u6b21\u89e6\u6478\u70b92(\u624b\u63072)\r\n    private void Rotate()\r\n    {\r\n\t\tif (Input.touchCount &lt;= 1) {\r\n\t\t\treturn;\r\n\t\t}\r\n \r\n\t\tTouch touch1 = Input.GetTouch(0);\r\n\t\tTouch touch2 = Input.GetTouch(1);\r\n \r\n\t\t\/\/\u542f\u7528\u53cc\u6307\uff0c\u5c1a\u672a\u65cb\u8f6c\r\n\t\tif (touch2.phase == TouchPhase.Began) {\r\n\t\t\toldTouch1 = touch1;\r\n\t\t\toldTouch2 = touch2;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tif (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) {\r\n\t\t\tVector2 curVec = touch2.position - touch1.position;\r\n\t\t\tVector2 oldVec = oldTouch2.position - oldTouch1.position;\r\n\t\t\tfloat angle = Vector2.Angle(oldVec, curVec);\r\n\t\t\tangle *= Mathf.Sign(Vector3.Cross(oldVec, curVec).z);\r\n \r\n\t\t\ttransform.Rotate(new Vector3(0, 0, angle) * rotateCo);\r\n \r\n\t\t\toldTouch1 = touch1;\r\n\t\t\toldTouch2 = touch2;\r\n\t\t}\r\n    }\r\n    \r\n    public void Reset() {\r\n        transform.localEulerAngles = Vector3.zero;\r\n    }\r\n}\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6548\u679c\u5982\u56fe\uff1a \u4e0a\u4ee3\u7801\uff1a \u5355\u6307\u79fb\u52a8 OneFingerMove.cs \u53cc\u6307\u7f29\u653e Tw &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=2213\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2213"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2213"}],"version-history":[{"count":1,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2213\/revisions"}],"predecessor-version":[{"id":2215,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2213\/revisions\/2215"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2213"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}