{"id":306,"date":"2015-04-20T16:33:51","date_gmt":"2015-04-20T08:33:51","guid":{"rendered":"http:\/\/qxu1606620110.my3w.com\/?p=306"},"modified":"2016-10-11T09:34:35","modified_gmt":"2016-10-11T01:34:35","slug":"%e4%bd%bf%e7%94%a8ngui%e5%88%b6%e4%bd%9c%e6%96%b9%e5%90%91%e6%91%87%e6%9d%86","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=306","title":{"rendered":"\u4f7f\u7528NGUI\u5236\u4f5c\u65b9\u5411\u6447\u6746"},"content":{"rendered":"<p><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2015\/04\/NGUIJoystick.gif\" data-lightbox=\"image_lg\"><img title=\"\u4f7f\u7528NGUI\u5236\u4f5c\u65b9\u5411\u6447\u6746 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"\u4f7f\u7528NGUI\u5236\u4f5c\u65b9\u5411\u6447\u6746 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" class=\"aligncenter wp-image-315 size-full\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2015\/04\/NGUIJoystick.gif\" width=\"528\" height=\"274\" \/><\/a><br \/>\n\u76f8\u4fe1\u5728\u770b\u672c\u7bc7\u6587\u7ae0\u7684\u670b\u53cb\u4e00\u5b9a\u5728\u505a\u624b\u6e38\uff0c\u505a\u624b\u6e38\u63a7\u5236\u89d2\u8272\u79fb\u52a8\u5c31\u4f1a\u7528\u5230\u6447\u6746\uff0c\u5927\u90e8\u5206\u670b\u53cb\u5e94\u8be5\u5df2\u7ecf\u4e86\u89e3\u8fc7EasyTouch\u8fd9\u6b3e\u63d2\u4ef6\u4e86\uff0cEasyTouch\u5df2\u7ecf\u51e0\u4e4e\u5c06\u6240\u6709\u9700\u8981\u7684\u64cd\u4f5c\u65b9\u5f0f\u90fd\u5c01\u88c5\u4e86\uff0c\u5df2\u7ecf\u7b97\u662f\u6bd4\u8f83\u597d\u7528\u7684\u6447\u6746\u63d2\u4ef6\u4e86\uff0c\u4f46EasyTouch\u5c01\u88c5\u7684\u65b9\u6cd5\u5185\u5bb9\u8f83\u591a\uff0c\u53ea\u662f\u4f1a\u7528\uff0c\u4f46\u53ef\u80fd\u4e0d\u80fd\u4e86\u89e3\u5176\u539f\u7406\u3002\u8fd9\u91cc \u6211\u4eec\u4e00\u8d77\u7528NGUI\u6765\u5b9e\u73b0\u4ee5\u4e0b\u6447\u6746\u7684\u5236\u4f5c\u3002<\/p>\n<p>\u539f\u7406\uff1a\u5728\u80cc\u666f\u56fe\u7247\u4e0a\u6dfb\u52a0Collider\u7ec4\u4ef6\uff0c\u5e76\u65b0\u5efa\u811a\u672c\u7ec4\u4ef6\uff0c\u6267\u884c\u65b9\u6cd5void OnPress \u6765\u68c0\u6d4b\u662f\u5426\u5728\u80cc\u666f\u56fe\u7247\u4e0a\u6709\u6309\u4e0b\u548c\u62ac\u8d77\u64cd\u4f5c\u3002\u5c06\u6309\u94ae\u56fe\u7247\u4f5c\u4e3a\u80cc\u666f\u56fe\u7247\u5b50\u7269\u4f53\uff0c \u5f53OnPress\u4e3aTrue\uff0c\u5219\u6309\u94ae\u56fe\u7247\u8ddf\u968f\u9f20\u6807\u79fb\u52a8\uff0c\u5f53OnPress\u4e3aFalse\uff0c\u5219\u6309\u94ae\u56fe\u7247\u8fd8\u539f\u5230\u4e2d\u5fc3\u70b9\uff0c\u901a\u8fc7\u8ba1\u7b97\u6309\u94ae\u56fe\u7247\u7684\u504f\u79fb\u91cf\u6765\u5b9e\u73b0\u7269\u4f53\u7684\u79fb\u52a8\u3002<\/p>\n<p>\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<p>\u80cc\u666f\u56fe\u7247\u4e0a\u7684\u811a\u672cJoystick<\/p>\n<pre class=\"lang:c# decode:true \">using UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class Joystick : MonoBehaviour {\r\n\tprivate bool isPress = false;\r\n\tTransform btnTran;\r\n\tGameObject player;\r\n\tpublic static float h, v;\r\n\r\n\tvoid Start (){\r\n\t        \/\/ btnTran\u4e3a\u5b50\u7269\u4f53\u6309\u94ae\u56fe\u7247\u7684Transform\r\n\t\tbtnTran = transform.GetChild (0);\r\n\t\tplayer = GameObject.FindGameObjectWithTag (Tags.player);\r\n\t}\r\n\r\n\tvoid Update (){\r\n\t\tPressIsTrue ();\r\n\t}\r\n\r\n\t\/\/ NGUI\u65b9\u6cd5 \u5f53\u9f20\u6807\u6309\u4e0b\u5f53\u524d\u7269\u4f53\u65f6\u4f1a\u89e6\u53d1\u4e00\u6b21\uff0c\u677e\u5f00\u65f6\u4e5f\u4f1a\u89e6\u53d1\u4e00\u6b21  \u4e00\u5171\u89e6\u53d1\u4e24\u6b21\r\n\t\/\/ \u53c2\u6570\u4e3a\u6309\u4e0b\u6216\u62ac\u8d77\u7684\u72b6\u6001\r\n\tvoid OnPress (bool press) {\r\n\t\tthis.isPress = press;\r\n\t\t\/\/ \u62ac\u8d77\u65f6\uff0c\u5c06\u4e2d\u5fc3\u6309\u94ae\u5750\u6807\u5f520\uff1b\r\n\t\tif (isPress == false){\r\n\t\t\tbtnTran.localPosition = Vector2.Lerp (btnTran.localPosition, Vector2.zero, 1f);\r\n\t\t\t\/\/ \u9f20\u6807\u62ac\u8d77\u65f6 \u5c06 h,v\u5f52\u96f6\r\n\t\t\th = 0;\r\n\t\t\tv = 0;\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/ \u6309\u4e0b\u65f6 \u89e6\u53d1\u6b64\u65b9\u6cd5\r\n\tvoid PressIsTrue (){\r\n\t\tif (isPress) {\r\n\t\t\t\/\/ UICamera.lastTouchPosition \u4e3a\u5f53\u524d\u9f20\u6807\u6309\u4e0b\u65f6\u7684\u5750\u6807\uff08Vector2\u7c7b\u578b\uff09\r\n\t\t\t\/\/ \u56e0\u4e3abutton\u4e2d\u5fc3\u70b9\u7684\u5750\u6807\u662fVector2(91,91)\uff0c \r\n\t\t\tVector2 touchPos = UICamera.lastTouchPosition - new Vector2(91, 91);\r\n\t\t\t\/\/ \u5f53\u9f20\u6807\u62d6\u52a8\u7684\u4f4d\u7f6e\u4e0e\u4e2d\u5fc3\u4f4d\u7f6e\u5927\u4e8e75\u65f6\uff0c\u5219\u56fa\u5b9a\u6309\u94ae\u4f4d\u7f6e\u4e0d\u4f1a\u8d85\u8fc775\u3002  75\u4e3a\u80cc\u666f\u56fe\u7247\u534a\u5f84\u957f\u5ea6\r\n\t\t\tif (Vector2.Distance (touchPos, Vector2.zero) &gt; 75) {\r\n\t\t\t\t\/\/ \u6309\u94ae\u4f4d\u7f6e\u4e3a \u9f20\u6807\u65b9\u5411\u5355\u4f4d\u5411\u91cf * 75\r\n\t\t\t\tbtnTran.localPosition = touchPos.normalized * 75;\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\t\/\/ \u6309\u94ae\u4f4d\u7f6e\u4e3a\u9f20\u6807\u4f4d\u7f6e\r\n\t\t\t\tbtnTran.localPosition = touchPos;\r\n\t\t\t}\r\n\t\t\t\/\/ \u6309\u94ae\u4f4d\u7f6ex\u8f74 \/ \u534a\u5f84 \u7684\u503c\u4e3a0-1\u7684\u6a2a\u5411\u504f\u79fb\u91cf\r\n\t\t\th = btnTran.localPosition.x \/ 75;\r\n\t\t\t\/\/ \u6309\u94ae\u4f4d\u7f6ey\u8f74 \/ \u534a\u5f84 \u7684\u503c\u4e3a0-1\u7684\u7eb5\u5411\u504f\u79fb\u91cf\r\n\t\t\tv = btnTran.localPosition.y \/ 75;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\u4e3b\u89d2\u8eab\u4e0a\u7684\u79fb\u52a8\u63a7\u5236\u811a\u672cPlayMove \u83b7\u53d6Joystick\u7684h,v\u7684\u503c<\/p>\n<pre class=\"lang:c# decode:true \">using UnityEngine;\r\nusing System.Collections;\r\npublic class PlayerMove : MonoBehaviour {\r\n\r\n\tprivate CharacterController cc;\r\n\tpublic float speed = 5f;\r\n\r\n\t\/\/ Use this for initialization\r\n\tvoid Start () {\r\n\t\tcc = GetComponent &lt;CharacterController&gt; ();\r\n\t}\r\n\t\r\n\t\/\/ Update is called once per frame\r\n\tvoid Update () {\r\n\/\/\t\tfloat h = Input.GetAxis (\"Horizontal\");\r\n\/\/\t\tfloat v = Input.GetAxis (\"Vertical\");\r\n\t\t\/\/ \u83b7\u53d6\u6447\u6746\u811a\u672c\u7684h,v\u7684\u503c\r\n\t\tfloat h = Joystick.h;\r\n\t\tfloat v = Joystick.v;\r\n\t\tif (Mathf.Abs(h) &gt; 0.1f || Mathf.Abs(v) &gt; 0.1f) {\r\n\t\t\tVector3 targetDir = new Vector3 (h, 0, v);\r\n\t\t\ttransform.LookAt (targetDir + new Vector3 (transform.position.x, transform.position.y, transform.position.z));\r\n\t\t\tcc.SimpleMove (targetDir * speed);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u4fe1\u5728\u770b\u672c\u7bc7\u6587\u7ae0\u7684\u670b\u53cb\u4e00\u5b9a\u5728\u505a\u624b\u6e38\uff0c\u505a\u624b\u6e38\u63a7\u5236\u89d2\u8272\u79fb\u52a8\u5c31\u4f1a\u7528\u5230\u6447\u6746\uff0c\u5927\u90e8\u5206\u670b\u53cb\u5e94 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=306\">\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":[77,78],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/306"}],"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=306"}],"version-history":[{"count":10,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":755,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions\/755"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}