Unity3D,如何为运营商编写扩展方法?

Evo*_*lor 1 c# extension-methods operator-overloading unity-game-engine

我试图扩展Vector3D功能的Vector3.它没有一个小于运算符,所以我试图创建一个.但是,当我为它编写扩展方法时,我的IDE告诉我"Identifier expected,'this'是一个关键字".

如何使用运算符编写扩展方法?这是我的尝试,出乎意料地无效:

using UnityEngine;
using System.Collections;

public static class Vector3Extensions
{
    public static bool operator <(this Vector3 vector3, Vector3 other)
    {
        if (vector3.x < other.x)
        {
            return true;
        }
        else if (vector3.x > other.x)
        {
            return false;
        }
        else if (vector3.y < other.y)
        {
            return true;
        }
        else if (vector3.y > other.y)
        {
            return false;
        }
        else if (vector3.z < other.z)
        {
            return true;
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ite 5

您不能使用扩展方法来重载运算符.也许你可以添加.LessThan.