{"id":1174,"date":"2017-06-30T17:41:30","date_gmt":"2017-06-30T09:41:30","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1174"},"modified":"2018-03-28T10:57:22","modified_gmt":"2018-03-28T02:57:22","slug":"%e8%a7%a3%e5%86%b3%e7%a7%bb%e5%8a%a8%e7%ab%af%e5%8f%8c%e6%91%87%e6%9d%86%e5%86%b2%e7%aa%81%e3%80%81%e6%91%87%e6%9d%86%e4%b8%8e%e6%8c%89%e9%92%ae%e5%86%b2%e7%aa%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1174","title":{"rendered":"\u89e3\u51b3\u79fb\u52a8\u7aef\u53cc\u6447\u6746\u51b2\u7a81\u3001\u6447\u6746\u4e0e\u6309\u94ae\u51b2\u7a81\u95ee\u9898"},"content":{"rendered":"<p>\u6700\u8fd1\u9879\u76ee\u9700\u6c42\u662f\u589e\u52a0\u53cc\u6447\u6746\uff0c\u7c7b\u4f3c\u738b\u8005\u8363\u8000\u4e2d\u7684\u5de6\u6447\u6746\u63a7\u5236\u89d2\u8272\u79fb\u52a8\uff0c\u53f3\u6447\u6746\u63a7\u5236\u6280\u80fd\u65b9\u5411\u3002<\/p>\n<p>\u9879\u76ee\u4e2d\u6ca1\u6709\u4f7f\u7528EasyTouch\uff0cFingerGestures\u7b49\u63d2\u4ef6\uff0c\u662f\u7eaf\u4ee3\u7801\u5b9e\u73b0\u7684\u53cc\u6447\u6746<\/p>\n<p>\u5f80\u5e38\u6211\u4eec\u5728PC\u4e2d\u83b7\u53d6\u70b9\u51fb\u4f4d\u7f6e\u662f\u7528Input.mousePosition\uff1b\u800c\u5728\u79fb\u52a8\u7aef\u8981\u83b7\u53d6\u591a\u70b9\u89e6\u63a7\uff0c\u9700\u7528\u5230Input.GetTouch\uff08\uff09\uff0c\u5982\u679c\u5bf9\u8be5\u65b9\u6cd5\u4e0d\u4e86\u89e3\u7684\uff0c\u8bf7\u81ea\u884c\u67e5\u8be2\u3002<\/p>\n<p>\u672c\u6b21\u6559\u7a0b\u5b9e\u73b0\u539f\u7406\uff1a\u5c06\u5c4f\u5e55\u5212\u5206\u5de6\u53f3\u533a\u57df\uff0c\u5bf9\u539f\u6765\u5355\u7eaf\u7684\u83b7\u53d6\u624b\u6307\u4f4d\u7f6e\uff0c\u6539\u4e3a\u83b7\u53d6\u5de6\u53f3\u533a\u57df\u4e2d\u7684\u624b\u6307\u4f4d\u7f6e\u3002<\/p>\n<p><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/06\/20160928100254613.jpg\" data-lightbox=\"image_lg\"><img title=\"\u89e3\u51b3\u79fb\u52a8\u7aef\u53cc\u6447\u6746\u51b2\u7a81\u3001\u6447\u6746\u4e0e\u6309\u94ae\u51b2\u7a81\u95ee\u9898 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"\u89e3\u51b3\u79fb\u52a8\u7aef\u53cc\u6447\u6746\u51b2\u7a81\u3001\u6447\u6746\u4e0e\u6309\u94ae\u51b2\u7a81\u95ee\u9898 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" class=\"aligncenter wp-image-1190\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/06\/20160928100254613.jpg\" width=\"580\" height=\"326\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/06\/20160928100254613.jpg 400w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/06\/20160928100254613-300x169.jpg 300w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>\u53cc\u6447\u6746\u4e2d\u7528\u5230\u7684\u70b9\u51fb\u4f4d\u7f6e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<\/p>\n<pre class=\"lang:c# decode:true\">GetPosition(TouchDirType.Left);<\/pre>\n<pre class=\"lang:c# decode:true\">GetPosition(TouchDirType.Right);<\/pre>\n<p>\u4e0b\u9762\u811a\u672c\u662f\u89e3\u51b3\u53cc\u6447\u6746\u51b2\u7a81\u7684\uff1a<\/p>\n<pre class=\"lang:c# decode:true\">using UnityEngine;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing System;\r\n\r\npublic enum TouchDirType\r\n{\r\n    Empty,\r\n    Left,\r\n    Right\r\n}\r\npublic class TouchManager : MonoBehaviour\r\n{\r\n\r\n    private static TouchManager instance;\r\n    public static TouchManager Instance { get { return instance; } }\r\n\r\n    float halfWith;\r\n    static Dictionary&lt;TouchDirType, Vector2&gt; dirTypeTouch;\r\n    Dictionary&lt;int, TouchDirType&gt; touchIndexType;\r\n\r\n    public Action&lt;TouchDirType&gt; onTouchBegan;       \/\/ \u624b\u6307\u6309\u4e0b\u65f6 \u4e8b\u4ef6\r\n    public Action&lt;TouchDirType&gt; onTouchMoved;       \/\/ \u624b\u6307\u6309\u4f4f\u65f6 \u4e8b\u4ef6\r\n    public Action&lt;TouchDirType&gt; onTouchEnded;       \/\/ \u624b\u6307\u677e\u5f00\u65f6 \u4e8b\u4ef6\r\n\r\n    void Awake()\r\n    {\r\n        if (instance == null)\r\n            instance = this;\r\n        else\r\n            Destroy(this);\r\n    }\r\n\r\n    \/\/ Use this for initialization\r\n    void Start()\r\n    {\r\n        halfWith = Screen.width * 0.5f;\r\n\r\n        dirTypeTouch = new Dictionary&lt;TouchDirType, Vector2&gt;();\r\n        dirTypeTouch.Add(TouchDirType.Left, Vector2.zero);\r\n        dirTypeTouch.Add(TouchDirType.Right, Vector2.zero);\r\n\r\n        touchIndexType = new Dictionary&lt;int, TouchDirType&gt;();\r\n    }\r\n\r\n    void LateUpdate()\r\n    {\r\n        for (int i = 0, length = Input.touchCount &gt; 2 ? 2 : Input.touchCount; i &lt; length; i++)\r\n        {\r\n            Touch touch = Input.GetTouch(i);\r\n\r\n            if (touch.phase == TouchPhase.Began)\r\n            {\r\n                if (touch.position.x &lt; halfWith)\r\n                {\r\n                    if (touchIndexType.ContainsKey(touch.fingerId))\r\n                    {\r\n                        touchIndexType[touch.fingerId] = TouchDirType.Left;\r\n                    }\r\n                    else\r\n                    {\r\n                        touchIndexType.Add(touch.fingerId, TouchDirType.Left);\r\n                    }\r\n                }\r\n                else\r\n                {\r\n                    if (touchIndexType.ContainsKey(touch.fingerId))\r\n                    {\r\n                        touchIndexType[touch.fingerId] = TouchDirType.Right;\r\n                    }\r\n                    else\r\n                    {\r\n                        touchIndexType.Add(touch.fingerId, TouchDirType.Right);\r\n                    }\r\n                }\r\n                dirTypeTouch[touchIndexType[touch.fingerId]] = touch.position;\r\n                if (onTouchBegan != null)\r\n                    onTouchBegan(touchIndexType[touch.fingerId]);\r\n\r\n            }\r\n            else if (touch.phase == TouchPhase.Moved)\r\n            {\r\n                if (touchIndexType.ContainsKey(touch.fingerId))\r\n                {\r\n                    dirTypeTouch[touchIndexType[touch.fingerId]] = touch.position;\r\n                    if (onTouchMoved != null)\r\n                        onTouchMoved(touchIndexType[touch.fingerId]);\r\n                }\r\n            }\r\n            else if (touch.phase == TouchPhase.Ended)\r\n            {\r\n\r\n                if (touchIndexType.ContainsKey(touch.fingerId))\r\n                {\r\n                    if (onTouchEnded != null)\r\n                        onTouchEnded(touchIndexType[touch.fingerId]);\r\n                    dirTypeTouch[touchIndexType[touch.fingerId]] = Vector2.zero;\r\n                    touchIndexType.Remove(touch.fingerId);\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u901a\u8fc7\u65b9\u5411\u83b7\u53d6\u8f93\u5165\u7684\u5750\u6807\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"dirType\"&gt;&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    public static Vector2 GetPosition(TouchDirType dirType)\r\n    {\r\n#if UNITY_EDITOR\r\n        return Input.mousePosition;\r\n#elif UNITY_ANDROID || UNITY_IPHONE\r\n        return dirTypeTouch[dirType];\r\n#endif\r\n    }\r\n\r\n    public static string leftOffset;\r\n    public static string rightOffset;\r\n    void OnGUI()\r\n    {\r\n        GUIStyle bb = new GUIStyle();\r\n        bb.normal.background = null;\r\n        bb.normal.textColor = new Color(1, 0, 0);\r\n        bb.fontSize = 40;\r\n\r\n        GUI.Label(new Rect(0, 0, 500, 50), \"LeftPosition: \" + GetPosition(TouchDirType.Left), bb);\r\n\r\n        GUI.Label(new Rect(0, 80, 500, 50), \"RightPosition: \" + GetPosition(TouchDirType.Right), bb);\r\n\r\n        GUI.Label(new Rect(0, 160, 500, 50), \"LeftOffset: \" + leftOffset, bb);\r\n\r\n        GUI.Label(new Rect(0, 240, 500, 50), \"RightOffset: \" + rightOffset, bb);\r\n    }\r\n\r\n}<\/pre>\n<p>\u4e0b\u9762\u811a\u672c\u662f\u5b9e\u73b0\u53cc\u6447\u6746\u7684<\/p>\n<pre class=\"lang:c# decode:true \">using UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class Joystick : MonoBehaviour\r\n{\r\n    public TouchDirType touchDirType;\r\n\r\n    bool isPress = false;\r\n\tbool touchActionActive = false;\r\n    float h, v;\r\n    public float bigCircleRadius = 100;\r\n\r\n    Transform bigCircleTrans;\r\n    Transform smallCircleTrans;\r\n\r\n    Vector2 bigCircleStartWorldPos = Vector2.zero;\r\n    Vector2 smallCircleStartLocalPos = Vector2.zero;\r\n\r\n    Vector2 startPressPos;      \/\/ \u6280\u80fd\u6447\u6746\u6309\u4e0b\u65f6\u7684\u521d\u59cb\u4f4d\u7f6e \u5373\u7b2c\u4e00\u5e27\u4f4d\u7f6e\r\n\r\n    private Vector2 offset;     \/\/ \u6447\u6746\u7684\u504f\u79fb\u91cf  -1\u52301\r\n    public Vector2 GetOffset()\r\n    {\r\n        return new Vector2(h, v);\r\n    }\r\n\r\n    void Start()\r\n    {\r\n\t\t\r\n\t\t#if UNITY_ANDROID || UNITY_IPHONE\r\n\t\tTouchManager.Instance.onTouchBegan += OnTouchBegan;        \r\n\t\t#endif\r\n\r\n        bigCircleTrans = transform;\r\n        smallCircleTrans = transform.GetChild(0);\r\n        smallCircleStartLocalPos = smallCircleTrans.localPosition;\r\n    }\r\n\r\n\tvoid Destroy()\r\n\t{\r\n\t\t\r\n\t\t#if UNITY_ANDROID || UNITY_IPHONE\r\n\t\tTouchManager.Instance.onTouchBegan -= OnTouchBegan;        \r\n\t\t#endif\r\n\t}\r\n\r\n    void Update()\r\n    {\r\n        if (isPress)\r\n        {\r\n            PressIsTrue();\r\n        }\r\n\r\n        switch (touchDirType)\r\n        {\r\n            case TouchDirType.Empty:\r\n                break;\r\n            case TouchDirType.Left:\r\n                TouchManager.leftOffset = GetOffset().ToString();\r\n                break;\r\n            case TouchDirType.Right:\r\n                TouchManager.rightOffset = GetOffset().ToString();\r\n                break;\r\n            default:\r\n                break;\r\n        }\r\n    }\r\n    \r\n    public void OnPointDown()\r\n    {\r\n\t\t#if UNITY_EDITOR\r\n        this.isPress = true;\r\n        startPressPos = TouchManager.GetPosition(touchDirType);\r\n\t\t#endif\r\n\t\ttouchActionActive = true;\r\n    }\r\n\r\n    public void OnPointUp()\r\n    {\r\n        this.isPress = false;\r\n        smallCircleTrans.localPosition = smallCircleStartLocalPos;\r\n\r\n\t\ttouchActionActive = false;\r\n        \/\/ \u9f20\u6807\u62ac\u8d77\u65f6 \u5c06 h,v\u5f52\u96f6\r\n        h = 0;\r\n        v = 0;\r\n    }\r\n\r\n#region \u624b\u673a\u7aef\u4f7f\u7528\r\n    public void OnTouchBegan(TouchDirType dirType)\r\n    {\r\n\t\tif (dirType != touchDirType || touchActionActive == false )\r\n            return;\r\n\r\n        isPress = true;\r\n        startPressPos = TouchManager.GetPosition(touchDirType);\r\n    }\r\n#endregion\r\n\r\n\r\n    \/\/ \u6309\u4e0b\u65f6 \u89e6\u53d1\u6b64\u65b9\u6cd5\r\n    void PressIsTrue()\r\n    {\r\n        \/\/ UICamera.lastTouchPosition \u4e3a\u5f53\u524d\u9f20\u6807\u6309\u4e0b\u65f6\u7684\u5750\u6807\uff08Vector2\u7c7b\u578b\uff09\r\n        if (bigCircleStartWorldPos == Vector2.zero)\r\n        {\r\n            bigCircleStartWorldPos = Camera.main.WorldToScreenPoint(bigCircleTrans.position);\r\n        }\r\n        Vector2 touchPos = TouchManager.GetPosition(touchDirType) - bigCircleStartWorldPos;\r\n        \/\/ \u5f53\u9f20\u6807\u62d6\u52a8\u7684\u4f4d\u7f6e\u4e0e\u4e2d\u5fc3\u4f4d\u7f6e\u5927\u4e8ebigCircleRadius\u65f6\uff0c\u5219\u56fa\u5b9a\u6309\u94ae\u4f4d\u7f6e\u4e0d\u4f1a\u8d85\u8fc7bigCircleRadius\u3002  bigCircleRadius\u4e3a\u80cc\u666f\u56fe\u7247\u534a\u5f84\u957f\u5ea6\r\n        if (Vector2.Distance(touchPos, Vector2.zero) &gt; bigCircleRadius)\r\n        {\r\n            \/\/ \u6309\u94ae\u4f4d\u7f6e\u4e3a \u9f20\u6807\u65b9\u5411\u5355\u4f4d\u5411\u91cf * bigCircleRadius\r\n            smallCircleTrans.localPosition = touchPos.normalized * bigCircleRadius;\r\n        }\r\n        else\r\n        {\r\n            \/\/ \u6309\u94ae\u4f4d\u7f6e\u4e3a\u9f20\u6807\u4f4d\u7f6e\r\n            smallCircleTrans.localPosition = touchPos;\r\n        }\r\n\r\n        \/\/ \u6309\u94ae\u4f4d\u7f6ex\u8f74 \/ \u534a\u5f84 \u7684\u503c\u4e3a0-1\u7684\u6a2a\u5411\u504f\u79fb\u91cf\r\n        h = smallCircleTrans.localPosition.x \/ bigCircleRadius;\r\n\r\n        \/\/ \u6309\u94ae\u4f4d\u7f6ey\u8f74 \/ \u534a\u5f84 \u7684\u503c\u4e3a0-1\u7684\u7eb5\u5411\u504f\u79fb\u91cf\r\n        v = smallCircleTrans.localPosition.y \/ bigCircleRadius;\r\n    }\r\n}<\/pre>\n<p>\u9879\u76ee\u4f7f\u7528\u7248\u672c\uff1aUnity5.3.4 GitHub\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/654306663\/TwoJoystick\">https:\/\/github.com\/654306663\/TwoJoystick<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u9879\u76ee\u9700\u6c42\u662f\u589e\u52a0\u53cc\u6447\u6746\uff0c\u7c7b\u4f3c\u738b\u8005\u8363\u8000\u4e2d\u7684\u5de6\u6447\u6746\u63a7\u5236\u89d2\u8272\u79fb\u52a8\uff0c\u53f3\u6447\u6746\u63a7\u5236\u6280\u80fd\u65b9\u5411 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1174\">\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":[23],"tags":[263,264],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1174"}],"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=1174"}],"version-history":[{"count":8,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1729,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1174\/revisions\/1729"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}