標籤:unity3d extension methods 擴充 unity
擴充方法的條件:
必須聲明為靜態類
必須聲明為靜態方法
方法的第一個參數為this
首先來擴充Unity中的協程Coroutine,
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; }}
再來擴充Transform組件的內容,特別是我們相對x,y, z 單獨賦值時,有了它是不是方便多了! 就是解決類似於 這樣的問題!
transform.position = new Vector3(5f,transform.position.y, transform.position.z);
看看怎麼擴充呢?
using UnityEngine;using System.Collections;public static class ExtensionsTransform{// 擴充了只修改一個值的 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); }// 擴充了更快的擷取一個值 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; }}
看看使用。
using UnityEngine;using System.Collections;public class Player : MonoBehaviour { void Update () { //Set new x position to 5 transform.SetPositionX(5f);//效果是一樣的 }}
還可以這樣擴充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;}}
接下來看看wiki 上的擴充四元數Quaternion
http://wiki.unity3d.com/index.php/QuaternionExtensions
??
Unity3d的C#擴充方法Extension methods應用吧