{"id":431,"date":"2021-01-25T22:25:37","date_gmt":"2021-01-25T14:25:37","guid":{"rendered":"http:\/\/lonelinerd.com\/?p=431"},"modified":"2021-05-08T02:48:27","modified_gmt":"2021-05-07T18:48:27","slug":"extension-methods","status":"publish","type":"post","link":"https:\/\/lonelinerd.com\/index.php\/2021\/01\/25\/extension-methods\/","title":{"rendered":"[C#\/Unity] \u5982\u4f55\u5beb\u64f4\u5c55\u65b9\u6cd5\uff08Extension Methods\uff09\u63d0\u9ad8\u958b\u767c\u6548\u7387"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"431\" class=\"elementor elementor-431\">\n\t\t\t\t\t\t<div class=\"elementor-inner\">\n\t\t\t\t<div class=\"elementor-section-wrap\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4fddda3 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4fddda3\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3a644fb\" data-id=\"3a644fb\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-63f7082 elementor-widget elementor-widget-text-editor\" data-id=\"63f7082\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p>\u00a0 \u00a0 \u00a0 \u00a0 \u64f4\u5c55\u65b9\u6cd5\uff08Extension Methods\uff09\u662f\u5c0d\u73fe\u6709\u7684\u6578\u64da\u985e\u578b\u5be6\u73fe\u4e00\u4e9b\u81ea\u5b9a\u7fa9\u7684\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u5584\u7528\u64f4\u5c55\u65b9\u6cd5\u80fd\u6709\u6548\u5730\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u6e1b\u5c11\u5f88\u591a\u5197\u9918\u7684\u4ee3\u78bc\u3002\u5c31\u6211\u81ea\u5df1\u800c\u8a00\uff0c\u5176\u4e2d\u4e00\u500b\u6700\u5927\u7684\u7528\u9014\u83ab\u904e\u65bc\u5728Unity\u7684\u4f7f\u7528\u4e0a\uff0c\u5c0dVector3\u3001Vector2\u3001Color\u7b49\u6578\u64da\u985e\u578b\u9032\u884c\u66f4\u4fbf\u6377\u7684\u8b8a\u63db\u3002<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u5728\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c\u9019\u985e\u6578\u64da\u985e\u578b\u7684\u8b8a\u63db\u7684\u4ee3\u78bc\u90fd\u6703\u986f\u5f97\u975e\u5e38\u591a\u9918\u3002\u6bd4\u5982\u6211\u60f3\u5c0d\u7269\u9ad4\u7684\u7576\u524dPosition\u7684Z\u8ef8\u589e\u52a010\uff0c\u6211\u662f\u4e0d\u53ef\u4ee5\u76f4\u63a5\uff1a<\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d0d1157 elementor-widget elementor-widget-text-editor\" data-id=\"d0d1157\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p style=\"padding-left: 40px;\"><code>transform.position.z += 10;<\/code><\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-953ce7a elementor-widget elementor-widget-text-editor\" data-id=\"953ce7a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p>&nbsp; &nbsp; &nbsp; &nbsp; \u800c\u5fc5\u9808\uff1a<\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3613107 elementor-widget elementor-widget-text-editor\" data-id=\"3613107\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p style=\"padding-left: 40px;\"><code>transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 10);<\/code><\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-22f6802 elementor-widget elementor-widget-text-editor\" data-id=\"22f6802\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<p>\u00a0 \u00a0 \u00a0 \u00a0 \u9019\u6a23\u4e00\u500b\u7c21\u55ae\u7684\u8b8a\u63db\uff0c\u90fd\u8981\u5beb\u4e00\u6bb5\u9019\u9ebc\u9577\u7684\u4ee3\u78bc\uff0c\u8cbb\u6642\u8cbb\u529b\uff0c\u4f46\u53ea\u8981\u5c0dVector3\u5beb\u4e00\u500b\u64f4\u5c55\u65b9\u6cd5\uff0c\u4e00\u5207\u90fd\u6703\u8b8a\u5f97\u7c21\u55ae\u5f88\u591a\u3002<\/p><h4><span style=\"text-decoration: underline;\"><strong>\u64f4\u5c55\u65b9\u6cd5\u7684\u5177\u9ad4\u5beb\u6cd5<\/strong><\/span><\/h4>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c2bcfa2 elementor-widget elementor-widget-text-editor\" data-id=\"c2bcfa2\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h5><strong>\u4e00\u3001\u9996\u5148\u5275\u5efa\u4e00\u500b\u975c\u614b\u985e<\/strong><\/h5><p>\u00a0 \u00a0 \u00a0 \u00a0 public static class ExtensionMethods{ }<\/p><p>\u00a0 \u00a0\u00a0<\/p><h5><strong>\u4e8c\u3001\u5b9a\u7fa9\u4e00\u500b\u975c\u614b\u65b9\u6cd5\uff08\u6211\u5728\u9019\u88e1\u5c31\u4ee5Vector3\u985e\u578b\u7528\u65bc\u5feb\u6377\u4fee\u6539z\u503c\u7684\u64f4\u5c55\u65b9\u6cd5\u70ba\u4f8b\uff09<\/strong><\/h5><p>\u00a0 \u00a0 \u00a0 \u00a0 public static class ExtensionMethods<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 {<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 public static Vector3 Z(){ }<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 }<\/p><p>\u00a0 \u00a0\u00a0<\/p><h5><strong>\u4e09\u3001\u5b9a\u7fa9\u65b9\u6cd5\u9700\u8981\u7684\u5f62\u53c3<\/strong><\/h5><p>\u00a0 \u00a0 \u00a0 \u00a0 public static class ExtensionMethods<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 {<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 public static Vector3 Z(this Vector3 value, float z){ }<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 }<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #ff0000;\">*this Vector3 value \u9019\u500b\u53c3\u6578\u662f\u4f7f\u7528\u6642\u81ea\u52d5\u50b3\u503c\u7684\uff0c\u4ee3\u8868\u8457\u4f7f\u7528\u9019\u500b\u64f4\u5c55\u65b9\u6cd5\u7684Vector3\u6578\u64da\u3002\u6bd4\u5982transform.position.Z\uff0c\u90a3\u9019\u88e1\u7684value\u5c31\u662ftransform.position\u81ea\u8eab\u3002<\/span><\/p><p>\u00a0 \u00a0 \u00a0 \u00a0*\u5f8c\u9762\u7684float z\u5c31\u662f\u5177\u9ad4\u60f3\u6539\u8b8a\u7684\u76ee\u6a19\u503c\u4e86<\/p><p>\u00a0 \u00a0\u00a0<\/p><h5><strong>\u56db\u3001\u6700\u5f8c\u5c31\u662f\u7de8\u5beb\u5177\u9ad4\u7684\u51fd\u6578\u9ad4\u4e86\uff08\u9019\u88e1\u4ee5\u5feb\u6377\u4fee\u6539z\u503c\u70ba\u4f8b\uff09<\/strong><\/h5><p>\u00a0 \u00a0 \u00a0 \u00a0 public static class ExtensionMethods<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 {<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 public static Vector3 Z(this Vector3 value, float z)<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 {<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Vector3 newValue = value;<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 newValue.z = z;<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return newValue;<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 }<\/p><p>\u00a0 \u00a0\u00a0<\/p><h5><strong>\u4e94\u3001\u9019\u6a23\u5beb\u5b8c\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u9019\u7a2e\u7c21\u6f54\u7684\u65b9\u6cd5\u5c0d\u4f4d\u7f6e\u4fe1\u606f\u9032\u884c\u4fee\u6539\u4e86<\/strong><\/h5><p>\u00a0 \u00a0 \u00a0 \u00a0 transform.position = transform.position.Z(transform.position.z + 10);<\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-39a97f1 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"39a97f1\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t\t\t<div class=\"elementor-row\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-dbe58ac\" data-id=\"dbe58ac\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-column-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t<div class=\"elementor-widget-wrap\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-e35b443 elementor-widget-divider--view-line elementor-widget elementor-widget-divider\" data-id=\"e35b443\" data-element_type=\"widget\" data-widget_type=\"divider.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<div class=\"elementor-divider\">\n\t\t\t<span class=\"elementor-divider-separator\">\n\t\t\t\t\t\t<\/span>\n\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e26d704 elementor-widget elementor-widget-text-editor\" data-id=\"e26d704\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-text-editor elementor-clearfix\">\n\t\t\t\t<h4><span style=\"text-decoration: underline;\"><strong>\u4e00\u9ede\u7e3d\u7d50<\/strong><\/span><\/h4><p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e0a\u9762\u4fee\u6539Vector3 z\u503c\u53ea\u662f\u4e00\u500b\u5f88\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u9664\u4e86Vector3\u9019\u4e9bunity\u7279\u6709\u7684\u6578\u64da\u985e\u578b\u4ee5\u5916\uff0cint\u3001float\u3001string\u7b49\u7b49\u57fa\u672c\u7684C#\u6578\u64da\u985e\u578b\u4e5f\u53ef\u4ee5\u901a\u904e\u4e0a\u9762\u7684\u6b65\u9a5f\u5beb\u4e00\u4e9b\u63d0\u9ad8\u958b\u767c\u6548\u7387\u7684\u64f4\u5c55\u65b9\u6cd5\u3002<\/p><p>\u00a0 \u00a0 \u00a0 \u00a0 \u64f4\u5c55\u65b9\u6cd5\u5be6\u969b\u4e0a\u4e26\u6c92\u6709\u63d0\u9ad8\u904b\u884c\u7684\u6548\u7387\uff0c\u5c31\u7d50\u679c\u800c\u8a00\uff0c\u4ecd\u7136\u662fnew\u4e86\u4e00\u500bVector3\uff0c\u7136\u5f8c\u8ce6\u503c\uff0c\u518d\u8fd4\u56de\u51fa\u53bb\uff0c\u4f46\u662f\u53ea\u8981\u6709\u4e86\u9019\u500bshortcut\uff0c\u5c31\u80fd\u91cd\u8986\u4f7f\u7528\uff0c\u5927\u5e45\u63d0\u9ad8\u5beb\u4ee3\u78bc\u7684\u6548\u7387\u3002\u5176\u5be6\u9019\u7a2e\u5c07\u91cd\u8986\u7684\u5de5\u4f5c\u62bd\u8c61\u51fa\u4f86\uff0c\u5c01\u88dd\u6210\u4e00\u500b\u65b9\u6cd5\uff0c\u5f9e\u800c\u63d0\u9ad8\u4ee3\u78bc\u7684\u8907\u7528\u6027\uff0c\u4e5f\u662f\u6211\u5011\u505a\u9762\u5411\u5c0d\u8c61\u7de8\u7a0b\u6642\u7684\u4e00\u500b\u6838\u5fc3\u601d\u60f3\u3002<\/p>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u64f4\u5c55\u65b9\u6cd5\uff08Extension Methods\uff09\u662f\u5c0d\u73fe\u6709\u7684\u6578\u64da\u985e\u578b\u5be6\u73fe\u4e00\u4e9b\u81ea\u5b9a\u7fa9\u7684\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u5584\u7528\u64f4\u5c55\u65b9\u6cd5\u80fd\u6709\u6548\u5730\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u6e1b\u5c11\u5f88\u591a\u5197\u9918\u7684\u4ee3\u78bc\u3002<\/p>\n","protected":false},"author":1,"featured_media":432,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,17],"tags":[],"class_list":["post-431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-notes","category-devnotes"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/lonelinerd.com\/wp-content\/uploads\/2021\/01\/FeatureCover_ExtensionMethods.png","_links":{"self":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":25,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":1306,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/posts\/431\/revisions\/1306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/media\/432"}],"wp:attachment":[{"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lonelinerd.com\/index.php\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}