I've seen this before, but I haven't really used it, I hope it will be used
It probably means that in C #, especially when using unity, the existing framework provides us with some fixed methods, but sometimes we want to modify these methods,
At this point we use the expansion method , also known as C # syntax sugar.
However, there are several points to note:
- The class must be static, that is, still class, but cannot inherit the Monobehaviour class.
- Methods in static classes also need to be static.
- The parameters passed in need to be decorated with this modifier, such as public static void Setpositionx (this Transform trans) {}
- If the write extension method is in a namespace, it is necessary to introduce a namespace when other classes are used, such as using Extends;
Below the code directly, relatively simple, understand the meaning can be.
1 usingUnityengine;2 usingSystem.Collections;3 namespaceExtends {//Note namespace4 Public Static classExtendtransform {//Static class5 Public Static voidSetpositionx ( ThisTransform Trans,floatx) {//static method, note the location of this6Trans.position =NewVector3 (x, TRANS.POSITION.Y, trans.position.z);7 }8 Public Static BOOLHavezero ( ThisTransform Trans) {//can have return value9Return (trans.position==new Vector3 (0,0,0));Ten } One } A}
The above is the definition code for the extension method, followed by the application.
1 usingUnityengine;2 usingSystem.Collections;3 usingExtends; Introducing an extension method namespace4 Public classTest:monobehaviour {5 PublicGameobject obj;6 voidStart () {7 Debug.Log (obj.transform.position);8Obj.transform.SetPositionX (Ten); Calling methods directly9 }Ten}
Then paste some examples from the Internet to help you understand. The specific author is unknown.
1 usingUnityengine; 2 usingSystem.Collections; 3 4 Public Static classExtensions5 { 6 Public Static voidSetpositionx ( ThisTransform T,floatnewx)7 { 8T.position =NewVector3 (newx, T.POSITION.Y, t.position.z); 9 } Ten One Public Static voidSetpositiony ( ThisTransform T,floatnewy) A { -T.position =NewVector3 (t.position.x, Newy, t.position.z); - } the - Public Static voidSetpositionz ( ThisTransform T,floatNewz) - { -T.position =NewVector3 (t.position.x, T.POSITION.Y, Newz); + } - + Public Static floatGetpositionx ( ThisTransform t) A { at returnt.position.x; - } - - Public Static floatGetpositiony ( ThisTransform t) - { - returnT.POSITION.Y; in } - to Public Static floatGetpositionz ( ThisTransform t) + { - returnt.position.z; the } * $ Public Static BOOLHasrigidbody ( Thisgameobject gobj)Panax Notoginseng { - return(Gobj.rigidbody! =NULL); the } + A Public Static BOOLHasanimation ( Thisgameobject gobj) the { + return(Gobj.animation! =NULL); - } $ $ Public Static voidSetspeed ( ThisAnimation Anim,floatnewspeed) - { -Anim[anim.clip.name].speed =Newspeed; the } -}
Use:
1 usingUnityengine; 2 usingSystem.Collections; 3 4 Public classPlayer:monobehaviour5 { 6 voidUpdate ()7 { 8 floatCurrentX =transform. Getpositionx (); 9Transform. Setpositionx (CurrentX +5f); Ten if(Gameobject.hasrigidbody ()) One { A } - if(Gameobject.hasanimation ()) - { the gameObject.animation.SetSpeed (2f); - } - } -}
In the future, we will update some small knowledge points, hoping to improve together with you.
How to expand in Unity and C #