Unity教程之-Unity3d的C#扩展方法Extension methods应用

 

本节unity教程我们来学习下Unity3d的C#扩展方法Extension methods,下面我们继续

扩展方法的条件:

  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;
}
}

好了,本节unity教程关于unity中的扩展方法介绍到此结束!