当我想要在Unity中改变一个变换的位置时,通常会按照以下步骤进行:
var x = 10f;
transform.position = new Vector3(x, transform.position.y, transform.position.z);
但我认为这有点繁琐。因此,我在 Vector3 类中添加了此扩展方法。
public static class Vector3Extensions
{
public static void SetX(this Vector3 pos, float x)
{
pos = new Vector3(x, pos.y, pos.z);
}
....
当我调用时,没有错误,但实际上值没有改变。是的,我知道这可能是因为Vector3是结构体。我尝试向我的方法添加ref关键字,
public static void SetX(ref this Vector3 pos, float x)
{
pos = new Vector3(x, pos.y, pos.z);
}
但是它不起作用,因为出现了“属性或索引器不能作为 out 或 ref 参数传递”的错误。 我想要的效果就像这样:
transform.position.SetX(10f);
有什么方法吗?谢谢。
transform.SetXPos()
方法。谢谢。 - Shintaro Nomiya