Skip to content

Instantly share code, notes, and snippets.

@stilllisisi
Created March 11, 2020 07:35
Show Gist options
  • Save stilllisisi/b0e16c5f18dd9a956383357c2f1e086c to your computer and use it in GitHub Desktop.
Save stilllisisi/b0e16c5f18dd9a956383357c2f1e086c to your computer and use it in GitHub Desktop.
【游戏】利用This扩展静态方法(链式编程)
//比如 "gameObject.GetComponent<Transform>() ; " 可以通过GameObject直接调用Unity定义好的API "GetComponent" ;
//如果已有API满足不了使用需求,还可以利用This关键字进行扩展(专业名词应该是链式编程),当然也不仅限于GameObject。
//比如:
//修改Transform.Position的X值:
public static void SetPositionX(this Transform t, float newX)
{
t.position = new Vector3(newX, t.position.y, t.position.z);
}
//修改Vector3的Y值:
public static Vector3 SetY(this Vector3 t, float newY)
{
t.y = newY;
return t;
}
//修改Color的透明度:
public static Color SetAlpha(this Color c, float newAlpha)
{
c.a = newAlpha;
return c;
}
//这样就可以自己封装一些使用频率高的方法函数,调用更加简洁方便~~
@stilllisisi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment