Https://www.cnblogs.com/zhaoqingqing/p/4629336.html
Need to write less code
When we repeatedly write complex code, or some C # methods, we would like to be able to have a more convenient way? In unity, of course, we extend it.
There are two points to keep in mind about extending the class of unity or C # to the following:
1. This class must be declared as static, and the extended method must be declared as static
2, in use, you can directly call the extension method
Extend Unity's Properties demo
Using Unityengine;Using System.Collections;//it is common to create a class to contain all of your//extension methods. This class must is static. public static class extensionmethods{ //even Though they is used like normal methods, extension //met Hods must be declared static. Notice that the first //parameter have the ' this ' keyword followed by a Transform //variable. This variable denotes which class the extension //method becomes a part of. public static void Resettransformation (thisTransform trans) {trans.position = Vector3.zero; Trans.localrotation = quaternion.identity; Trans.localscale = New Vector3 (1, 1, 1);}}
How to use
Using Unityengine; using System.Collections; class Someclass:monobehaviour { void Start () {//passed in as the first parameter. Transform. Resettransformation (); }}
Ways to extend C #
Extend a method for a collection of C #, and when called, you can call Cfirstordefault directly
Static T cfirstordefault<t> (thisienumerable<t> source) { null) { Default (T);}
Document Information
Document: Http://unity3d.com/learn/tutorials/modules/intermediate/scripting/extension-methods
Ways to extend Unity