{"id":2088,"date":"2020-09-21T14:15:48","date_gmt":"2020-09-21T06:15:48","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=2088"},"modified":"2020-09-21T14:18:21","modified_gmt":"2020-09-21T06:18:21","slug":"unity-%e5%8f%8c%e6%91%87%e6%9d%86%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=2088","title":{"rendered":"Unity\u53cc\u6447\u6746\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img title=\"Unity\u53cc\u6447\u6746\u89e3\u51b3\u65b9\u6848 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u53cc\u6447\u6746\u89e3\u51b3\u65b9\u6848 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" width=\"853\" height=\"470\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2020\/09\/20200921_115323.gif\" alt=\"\" class=\"wp-image-2089\"\/><\/figure>\n\n\n\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\nusing UnityEngine.EventSystems;\n\npublic class JoystickCtrl : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler\n{\n    \/* component refs *\/\n    public RectTransform background;\n    public RectTransform handle;\n\n    \/* public vars *\/\n    public float handleLimit = 1f;\n    protected Vector2 inputVector = Vector2.zero;\n\n    public float Horizontal { get { return inputVector.x; } }\n    public float Vertical { get { return inputVector.y; } }\n    public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }\n\n    public virtual void OnDrag(PointerEventData eventData)\n    {\n\n    }\n\n    public virtual void OnPointerDown(PointerEventData eventData)\n    {\n\n    }\n\n    public virtual void OnPointerUp(PointerEventData eventData)\n    {\n\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing UnityEngine.Events;\nusing UnityEngine.EventSystems;\n\npublic class JoystickComponentCtrl : JoystickCtrl {\n\n    \/* component refs *\/\n    public RectTransform Container;\n\n    \/* private vars *\/\n    private Vector2 _joystickCenter = Vector2.zero;\n    private Vector3 _containerDefaultPosition;\n\n    public UnityEvent OnTap;\n\n    void Start()\n    {\n        this._containerDefaultPosition = this.Container.position;\n    }\n\n    public override void OnDrag(PointerEventData eventData)\n    {\n        Vector2 direction = eventData.position - _joystickCenter;\n        inputVector = (direction.magnitude > background.sizeDelta.x \/ 2f) ? direction.normalized : direction \/ (background.sizeDelta.x \/ 2f);\n        handle.anchoredPosition = (inputVector * background.sizeDelta.x \/ 2f) * handleLimit;\n    }\n\n    public override void OnPointerDown(PointerEventData eventData)\n    {\n        Container.position = eventData.position;\n        handle.anchoredPosition = Vector2.zero;\n        _joystickCenter = eventData.position;\n    }\n\n    public override void OnPointerUp(PointerEventData eventData)\n    {\n        Container.position = this._containerDefaultPosition;\n        handle.anchoredPosition = Vector2.zero;\n        inputVector = Vector2.zero;\n        this.OnTap.Invoke();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>GitHub\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/654306663\/TwoJoystickPro\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/654306663\/TwoJoystickPro<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4ee3\u7801\uff1a GitHub\u4e0b\u8f7d\u5730\u5740\uff1a https:\/\/github.com\/6543 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=2088\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2089,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2088"}],"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=2088"}],"version-history":[{"count":4,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2088\/revisions"}],"predecessor-version":[{"id":2095,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2088\/revisions\/2095"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/media\/2089"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}