{"id":99,"date":"2020-06-05T02:55:00","date_gmt":"2020-06-05T02:55:00","guid":{"rendered":"https:\/\/lucyestela.com\/dev\/?p=99"},"modified":"2021-03-02T03:00:44","modified_gmt":"2021-03-02T03:00:44","slug":"extension-classes","status":"publish","type":"post","link":"https:\/\/lucyestela.com\/dev\/unity\/extension-classes\/","title":{"rendered":"Extension Classes &#8211; Unity"},"content":{"rendered":"<div class=\"boldgrid-section\">\n<div class=\"container\">\n<div class=\"row\">\n<div class=\"col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\">So these are classes that you write, that extend one of the Unity classes. There&#8217;s a good example on one of the Learn Unity tutorial site <a href=\"https:\/\/learn.unity.com\/tutorial\/extension-methods\">here<\/a>. But basically&nbsp; you will create the class file like this:<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #009695;\">using<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">UnityEngine<\/span><span style=\"color: #222222;\">;<\/span><span style=\"color: #222222;\">&nbsp;<\/span>\r\n<span style=\"color: #009695;\">using<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">System<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">Collections<\/span><span style=\"color: #222222;\">;<\/span><span style=\"color: #222222;\">&nbsp;<\/span>\r\n\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/It&nbsp;is&nbsp;common&nbsp;to&nbsp;create&nbsp;a&nbsp;class&nbsp;to&nbsp;contain&nbsp;all&nbsp;of&nbsp;your&nbsp;<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/extension&nbsp;methods.&nbsp;This&nbsp;class&nbsp;must&nbsp;be&nbsp;static.&nbsp;<\/span>\r\n\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #009695;\">public<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">static<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">class<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">ExtensionMethods<\/span>\r\n<span style=\"color: #222222;\">{<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/RULE&nbsp;1&nbsp;:&nbsp;extension&nbsp;methods&nbsp;must&nbsp;be&nbsp;declared&nbsp;static.&nbsp;<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/RULE&nbsp;2&nbsp;:&nbsp;The&nbsp;first&nbsp;parameter&nbsp;must&nbsp;have&nbsp;the&nbsp;keyword&nbsp;\"this\"<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/RULE&nbsp;3&nbsp;:&nbsp;Follow&nbsp;the&nbsp;\"this\"&nbsp;with&nbsp;the&nbsp;Class&nbsp;name&nbsp;that&nbsp;you&nbsp;are&nbsp;extending,&nbsp;<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/&nbsp;example,&nbsp;Vector3,&nbsp;Transform,&nbsp;Rigidbody<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/RULE&nbsp;4&nbsp;:&nbsp;Follow&nbsp;the&nbsp;class&nbsp;type&nbsp;with&nbsp;a&nbsp;variable&nbsp;name&nbsp;to&nbsp;use&nbsp;in&nbsp;the&nbsp;method<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #888a85;\">\/\/&nbsp;then&nbsp;just&nbsp;do&nbsp;whatever&nbsp;you&nbsp;need&nbsp;to&nbsp;in&nbsp;the&nbsp;method&nbsp;as&nbsp;per&nbsp;usual<\/span>\r\n\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #009695;\">public<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">static<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">void<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">ResetTransformation<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #009695;\">this<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">Transform<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">trans<\/span><span style=\"color: #222222;\">)<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #222222;\">{<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #222222;\">trans<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">position<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">Vector3<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">zero<\/span><span style=\"color: #222222;\">;<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #222222;\">trans<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">localRotation<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">Quaternion<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">identity<\/span><span style=\"color: #222222;\">;<\/span>\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #222222;\">trans<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">localScale<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">new<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">Vector3<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #db7100;\">1<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #db7100;\">1<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #db7100;\">1<\/span><span style=\"color: #222222;\">);<\/span>\r\n\r\n<span style=\"color: #222222;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #222222;\">}<\/span>\r\n\r\n<span style=\"color: #222222;\">}<\/span><\/span><\/pre>\n<p class=\"\">Now you can refer to your new extension method with the following code<\/p>\n<pre><span style=\"font-family: Menlo;\"><span style=\"color: #888a85;\">\/\/Notice how you pass no parameter into this<\/span>\r\n<span style=\"color: #888a85;\">\/\/extension method even though you had one in the<\/span>\r\n<span style=\"color: #888a85;\">\/\/method&nbsp;declaration.&nbsp;The&nbsp;transform&nbsp;object&nbsp;that<\/span>\r\n<span style=\"color: #888a85;\">\/\/this&nbsp;method&nbsp;is&nbsp;called&nbsp;from&nbsp;automatically&nbsp;gets<\/span>\r\n<span style=\"color: #888a85;\">\/\/passed&nbsp;in&nbsp;as&nbsp;the&nbsp;first&nbsp;parameter.<\/span>\r\n<span style=\"color: #222222;\">transform<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">ResetTransformation<\/span><span style=\"color: #222222;\">();<\/span><\/span><\/pre>\n<p class=\"\">it is the same as saying<\/p>\n<pre><span style=\"font-family: Menlo;\">ExtensionMethods<span style=\"color: #222222;\">.ResetTransformation<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">yourTransform<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>So these are classes that you write, that extend one of the Unity classes. There&#8217;s a good example on one of the Learn Unity tutorial site here. But basically&nbsp; you will create the class file like this: using&nbsp;UnityEngine;&nbsp; using&nbsp;System.Collections;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;\/\/It&nbsp;is&nbsp;common&nbsp;to&nbsp;create&nbsp;a&nbsp;class&nbsp;to&nbsp;contain&nbsp;all&nbsp;of&nbsp;your&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;\/\/extension&nbsp;methods.&nbsp;This&nbsp;class&nbsp;must&nbsp;be&nbsp;static.&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;class&nbsp;ExtensionMethods { &nbsp;&nbsp;&nbsp;&nbsp;\/\/RULE&nbsp;1&nbsp;:&nbsp;extension&nbsp;methods&nbsp;must&nbsp;be&nbsp;declared&nbsp;static.&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;\/\/RULE&nbsp;2&nbsp;:&nbsp;The&nbsp;first&nbsp;parameter&nbsp;must&nbsp;have&nbsp;the&nbsp;keyword&nbsp;&#8220;this&#8221; &nbsp;&nbsp;&nbsp;&nbsp;\/\/RULE&nbsp;3&nbsp;:&nbsp;Follow&nbsp;the&nbsp;&#8220;this&#8221;&nbsp;with&nbsp;the&nbsp;Class&nbsp;name&nbsp;that&nbsp;you&nbsp;are&nbsp;extending,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;example,&nbsp;Vector3,&nbsp;Transform,&nbsp;Rigidbody &nbsp;&nbsp;&nbsp;&nbsp;\/\/RULE&nbsp;4&nbsp;:&nbsp;Follow&nbsp;the&nbsp;class&nbsp;type&nbsp;with&nbsp;a&nbsp;variable&nbsp;name&nbsp;to&nbsp;use&nbsp;in&nbsp;the&nbsp;method &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;then&nbsp;just&nbsp;do&nbsp;whatever&nbsp;you&nbsp;need&nbsp;to&nbsp;in&nbsp;the&nbsp;method&nbsp;as&nbsp;per&nbsp;usual &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;ResetTransformation(this&nbsp;Transform&nbsp;trans) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans.position&nbsp;=&nbsp;Vector3.zero; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans.localRotation&nbsp;=&nbsp;Quaternion.identity; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trans.localScale&nbsp;=&nbsp;new&nbsp;Vector3(1,&nbsp;1,&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;} &#8230; <a title=\"Extension Classes &#8211; Unity\" class=\"read-more\" href=\"https:\/\/lucyestela.com\/dev\/unity\/extension-classes\/\" aria-label=\"More on Extension Classes &#8211; Unity\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[58],"tags":[39,40,4],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-unity","tag-class","tag-extension","tag-unity"],"_links":{"self":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":5,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/99\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}