Unity3d C # extended method Extension methods application,
Extension Method conditions:
Must be declared as a static class
Must be declared as a static method
The first parameter of the method is this
First, expand the Coroutine in Unity,
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")/// return; /// Freeze();/// this.ExecuteLater(()=> Unfreeze(), 2f); // unfreezes the current gameObject 2 seconds from now./// }/// /// </code>/// </summary>public static class TimingExtension{ public delegate bool When(); /// <summary> /// Execute the given Action when <code>condition</code> returns <code>true</code>. /// condition will 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 condition) { m.StartCoroutine(ExecuteWhenCoroutine(action, condition)); } /// <summary> /// Execute the action after a delay of <code>seconds</code> /// </summary> /// <param name="action">Action.</param> /// <param name="seconds">Seconds.</param> public static void ExecuteLater(this MonoBehaviour m, Action action, float seconds) { m.StartCoroutine(ExecuteLaterCoroutine(action, seconds)); } /// <summary> /// Execute an action next frame /// </summary> /// <param name="m">M.</param> /// <param name="action">Action.</param> public static void ExecuteNextFrame(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(seconds); action(); } private static IEnumerator ExecuteNextFrameCoroutine(Action action) { yield return null; action(); } public static void Co(this MonoBehaviour m, Func<IEnumerator> coroutine) { m.StartCoroutine(CoCoroutine(coroutine)); } private static IEnumerator CoCoroutine(Func<IEnumerator> coroutine) { yield return coroutine; }}
Extend the content of the Transform component, especially when we assign values to x, y, and z separately. Is it much more convenient with it! Is to solve such problems!
Transform. position = new Vector3 (5f, transform. position. y, transform. position. z );
Let's see how to scale it out?
Using UnityEngine; using System. collections; public static class ExtensionsTransform {// extends the public static 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, newY, t. position. z);} public static void SetPositionZ (this Transform t, float newZ) {t. position = new Vector3 (t. position. x, t. position. y, newZ);} // expands to get a value faster. 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 ;}}
Let's see how to use it.
Using UnityEngine; using System. collections; public class Player: MonoBehaviour {void Update () {// Set new x position to 5 transform. setPositionX (5f); // The effect is the same }}
You can also extend 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;}}
Next, let's take a look at the extended four-element Quaternion on wiki.
Http://wiki.unity3d.com/index.php/QuaternionExtensions