Unity3d的C#擴充方法Extension methods應用吧

來源:互聯網
上載者:User

標籤:unity3d   extension   methods   擴充   unity   



擴充方法的條件:

  1. 必須聲明為靜態類

  2. 必須聲明為靜態方法

  3. 方法的第一個參數為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應用吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.