Created
March 11, 2020 07:35
-
-
Save stilllisisi/b0e16c5f18dd9a956383357c2f1e086c to your computer and use it in GitHub Desktop.
【游戏】利用This扩展静态方法(链式编程)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//比如 "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; | |
} | |
//这样就可以自己封装一些使用频率高的方法函数,调用更加简洁方便~~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gameinstitute.qq.com/community/detail/128015