Conditions for extension methods:
must be declared as a static class
must be declared as a static method
the first parameter of a method is This
first to expandUnitythe co-process inCoroutine,
Using unityengine;using system.collections;using system;///<summary>///A set of extension methods meant help with Common Coroutine cases. Example://<code>///void Ontriggerenter (Collider col) {//if (Col.gameObject.tag! = "Ice")//RET Urn Freeze ();///this. Executelater (() = Unfreeze (), 2f); Unfreezes the current Gameobject 2 seconds from now.///}/////</code>///</summary>public static class T imingextension{public delegate bool when (); <summary>//Execute The given Action when <code>condition</code> returns <CODE>TRUE&L T;/code>. Condition'll be evaluated every frame. </summary>//<param name= "action" >the action to execute</param>//<param name= " Condition ">Condition.</param> public static void Executewhen (this monobehaviour m, action action, when Conditi ON) {M.startcoroutine (executewheNcoroutine (action, condition)); }//<summary>//Execute The action after a delay of <code>seconds</code>//&L t;/summary>//<param name= "action" >Action.</param>///<param name= "seconds" >seconds. </param> public static void Executelater (this monobehaviour m, action action, float seconds) {M.STARTC Oroutine (Executelatercoroutine (action, seconds)); }//<summary>//Execute an action next frame///</summary>//<param name = "M" >M.</param>///<param Name= "action" >Action.</param> public static void Executenext Frame (this monobehaviour m, action action) {M.startcoroutine (Executenextframecoroutine (action)); } private static IEnumerator Executewhencoroutine (action action, when condition) {while (!condition ()) yield return null; Action (); } private Static IEnumerator Executelatercoroutine (action action, float seconds) {yield return new waitforseconds (SE Conds); Action (); } private static IEnumerator Executenextframecoroutine (Action action) {yield return null; Action (); } public static void Co (this monobehaviour m, func<ienumerator> coroutine) {m.startcoroutine (cocorout INE (coroutine)); } private static IEnumerator Cocoroutine (func<ienumerator> coroutine) {yield return coroutine; }}
again to expandTransformthe contents of the components, especially our relativex, y, zwhen assigning a value alone, it is more convenient to have it! is to solve something similar to such a problem!
transform.position = new Vector3 (5F,TRANSFORM.POSITION.Y, transform.position.z);
See how it expands?
Using unityengine;using system.collections;public static class extensionstransform{//expands the public static that modifies only one value void Setpositionx (this Transform T, float newx) { t.position = new Vector3 (newx, T.POSITION.Y, t.position.z);
} public static void Setpositiony (this Transform T, float newy) { t.position = new Vector3 (t.position.x, NE WY, t.position.z); } public static void Setpositionz (this Transform T, float Newz) { t.position = new Vector3 (t.position.x, T.positi ON.Y, Newz); } Extended faster get a value public static float Getpositionx (this Transform t) { return t.position.x; } public static float Getpositiony (this Transform t) { return t.position.y; } public static float Getpositionz (this Transform t) { return t.position.z; }}
Look at the use.
Using unityengine;using System.collections;public class Player:monobehaviour { void Update () { //set new X Position to 5 transform. Setpositionx (5f);//effect is the same }}
You can also extend this Transform !
Static public class Extensionstransform {//<summary>///Gets or add a component. Usage Example://Boxcollider Boxcollider = transform. Getoraddcomponent<boxcollider> ();///</summary>static public T getoraddcomponent<t> (this Component child) where t:component {T result = child. Getcomponent<t> (); if (result = = null) {result = Child.gameobject.addcomponent<t> ();} return result;}}
pick it up and see Wiki four-dollar extension on quaternion
Http://wiki.unity3d.com/index.php/QuaternionExtensions
??
Unity3d's C # extension method extension methods application Bar