通常你会发现你不能修改正在使用的那些类,无论它是基础的数据类型还是已有框架的一部分,它提供的方法让你困苦不堪。不过。。C# 提供了一种巧妙的方式来让你扩充已有的类,也就是我们今天要讲的扩展方法。
扩展方法由于很方便而被经常使用到,我们更愿意叫他语法糖豆(syntactic sugar),一个实用样例是Unity的Transform类,比如:你只想设置Transform.position中Vector3的x轴,而其它两个轴保持不变。
| 1 2 3 4 5 6 7 8 9 10 11 | using UnityEngine;using System.Collections; public class Player : MonoBehaviour {    void Update ()     {        //Set new x position to 5        transform.position = newVector3(5f, transform.position.y, transform.position.z);    }} | 
在这个样例中,如果你只设置Transform.position的x轴变量,编译会报错,所以你不得不把其余的y轴和z轴加上组成一个完整的Vector3。于是一个拓展方法比如SetPositionX() 就可以添加到Transform类中,从而让代码的可读性更好。
为了创建拓展方法,你需要创建一个静态类。此外,一个拓展方法的声明前缀也必须加上静态修饰符static,同时该方法的第一个参数为所操作的类型,且必须以this关键字修饰。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | using UnityEngine;using System.Collections; //Must be in a static class public static class Extensions{    //Function must be static    //First parameter has "this" in front of type    public static void SetPositionX(thisTransform t, float newX)    {        t.position = newVector3(newX, t.position.y, t.position.z);    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 | using UnityEngine;  using System.Collections;     public class Player : MonoBehaviour   {      void Update ()       {          //Set new x position to 5          transform.SetPositionX(5f);      }  } | 
这里有一些样例代码来帮你更好理解拓展方法,以作参考。
拓展:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using UnityEngine;  using System.Collections;     public static class Extensions  {      public static void SetPositionX(thisTransform t, float newX)      {          t.position = newVector3(newX, t.position.y, t.position.z);      }         public static void SetPositionY(thisTransform t, float newY)      {          t.position = newVector3(t.position.x, newY, t.position.z);      }         public static void SetPositionZ(thisTransform t, float newZ)      {          t.position = newVector3(t.position.x, t.position.y, newZ);      }         public static float GetPositionX(thisTransform t)      {          returnt.position.x;      }         public static float GetPositionY(thisTransform t)      {          returnt.position.y;      }         public static float GetPositionZ(thisTransform t)      {          returnt.position.z;      }         public static bool HasRigidbody(thisGameObject gobj)      {          return(gobj.rigidbody != null);      }         public static bool HasAnimation(thisGameObject gobj)      {          return(gobj.animation != null);      }         public static void SetSpeed(thisAnimation anim, float newSpeed)      {          anim[anim.clip.name].speed = newSpeed;       }  } | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using UnityEngine;  using System.Collections;     public class Player : MonoBehaviour   {      void Update ()       {          //move x position 5 units          float currentX = transform.GetPositionX();          transform.SetPositionX(currentX + 5f);             if(gameObject.HasRigidbody())          {              //Do something with physics!          }             if(gameObject.HasAnimation())          {              //Double the animation speed!              gameObject.animation.SetSpeed(2f);          }      }  } | 
原文:http://www.cnblogs.com/forlove/p/3785271.html