{"id":1152,"date":"2014-04-26T07:06:24","date_gmt":"2014-04-26T07:06:24","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1152"},"modified":"2014-04-26T07:06:24","modified_gmt":"2014-04-26T07:06:24","slug":"unity3d-c-extend-method","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1152\/","title":{"rendered":"Unity3D C# extend method."},"content":{"rendered":"<p>\u7d93\u5e38\u5fd8\u8a18\u5982\u4f55\u505a Extend method.\u7b46\u8a18\u4e00\u4e0b.<\/p>\n<pre class=\"brush:csharp\">using UnityEngine;\r\nusing System.Collections;\r\n\r\nnamespace Kit.Vector3Ext\r\n{\r\n\tpublic static class Vector3Ext\r\n\t{\r\n\t\t\/\/\/ &lt;summary&gt;Assign vector3.x&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"x\"&gt;The x coordinate.&lt;\/param&gt;\r\n\t\tpublic static void SetX(this Vector3 _this, float x)\r\n\t\t{\r\n\t\t\t_this.x = x;\r\n\t\t}\r\n\t\t\/\/\/ &lt;summary&gt;Assign vector3.y&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"y\"&gt;The y coordinate.&lt;\/param&gt;\r\n\t\tpublic static void SetY(this Vector3 _this, float y)\r\n\t\t{\r\n\t\t\t_this.y = y;\r\n\t\t}\r\n\t\t\/\/\/ &lt;summary&gt;Assign vector3.z&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"z\"&gt;The z coordinate.&lt;\/param&gt;\r\n\t\tpublic static void SetZ(this Vector3 _this, float z)\r\n\t\t{\r\n\t\t\t_this.z = z;\r\n\t\t}\r\n\t\t\/\/\/ &lt;summary&gt;Clamp Vector3.x value.&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"_min\"&gt;_min.&lt;\/param&gt;\r\n\t\t\/\/\/ &lt;param name=\"_max\"&gt;_max.&lt;\/param&gt;\r\n\t\tpublic static void ClampX(this Vector3 _this, float _min, float _max)\r\n\t\t{\r\n\t\t\t_this.x = Mathf.Clamp(_this.x, _min, _max);\r\n\t\t}\r\n\t\t\/\/\/ &lt;summary&gt;Clamp Vector3.x value.&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"_min\"&gt;_min.&lt;\/param&gt;\r\n\t\t\/\/\/ &lt;param name=\"_max\"&gt;_max.&lt;\/param&gt;\r\n\t\tpublic static void ClampY(this Vector3 _this, float _min, float _max)\r\n\t\t{\r\n\t\t\t_this.y = Mathf.Clamp(_this.y, _min, _max);\r\n\t\t}\r\n\t\t\/\/\/ &lt;summary&gt;Clamp Vector3.x value.&lt;\/summary&gt;\r\n\t\t\/\/\/ &lt;param name=\"_min\"&gt;_min.&lt;\/param&gt;\r\n\t\t\/\/\/ &lt;param name=\"_max\"&gt;_max.&lt;\/param&gt;\r\n\t\tpublic static void ClampZ(this Vector3 _this, float _min, float _max)\r\n\t\t{\r\n\t\t\t_this.z = Mathf.Clamp(_this.z, _min, _max);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\u91cd\u9ede\u662f<\/p>\n<ul>\n<li>static class<\/li>\n<li>static function<\/li>\n<li>function input, first parameter = this,<\/li>\n<\/ul>\n<p>\u800c\u529f\u6548\u5c31\u662f,\u65bc\u7a0b\u5e8f\u4e2d\u5ba3\u544a\u4e86 using Kit.Vector3Ext \u5f8c, \u5373\u53ef\u4ee5\u7528\u4efb\u4f55\u5df2\u5ba3\u544a\u7684\u00a0Vector3 variable \u4e2d\u8abf\u7528\u76f8\u95dc\u7684 class.<\/p>\n<pre class=\"brush:csharp\">Vector3 _test = Vector3.zero;\r\n_text.SetX(10f); \/\/ Vector3 = {10f,0f,0f}\r\n_text.SetY(20f); \/\/ Vector3 = {10f,20f,0f}\r\n_text.SetZ(30f); \/\/ Vector3 = {10f,20f,30f}\r\n_text.ClampX(1f,5f); \/\/ Vector3 = {5f,20f,30f}\r\n_text.ClampY(5f,10f); \/\/ Vector3 = {5f,10f,30f}\r\n_text.ClampZ(1f,5f); \/\/ Vector3 = {5f,10f,5f}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7d93\u5e38\u5fd8\u8a18\u5982\u4f55\u505a Extend method.\u7b46\u8a18\u4e00\u4e0b. using UnityEngine; usi &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1152","post","type-post","status-publish","format-standard","hentry","category-unity3d"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1152"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}