相关疑难解决方法(0)

扩展方法如何工作?

我想在Unity3d中为Vector3类创建一个扩展方法.但我似乎没有得到它.这就是我所拥有的:

public static class ExtensionMethods{

    public static Vector3 MaxValue(this Vector3 _vec3)
    {
        return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想打一个Vector3.MaxValue,就像float.MaxValue用这行代码:

Vector3 closestPoint = Vector3.MaxValue;
Run Code Online (Sandbox Code Playgroud)

但后来我得到了这个错误:

error CS0117: `UnityEngine.Vector3' does not contain a definition for `MaxValue'
Run Code Online (Sandbox Code Playgroud)

我知道这会奏效:

Vector3 closestPoint = new Vector3().MaxValue();
Run Code Online (Sandbox Code Playgroud)

但后来我创建了2个新Vector3实例.一个在MaxValue通话中,一个在外面.是不是可以只使用这种代码制作一个并执行此操作:

Vector3 closestPoint = Vector3.MaxValue;
Run Code Online (Sandbox Code Playgroud)

c# extension-methods unity-game-engine

0
推荐指数
1
解决办法
1006
查看次数

标签 统计

c# ×1

extension-methods ×1

unity-game-engine ×1