我有一个继承MonoBehaviour的类。GameObject的扩展方法工作,但无用于Monobehaviour。
类文件
namespace UnityEngine
{
public class Test : MonoBehaviour
{
private void Start()
{
TestMono();
gameObject.TestObject();
}
}
}
扩展文件
namespace UnityEngine
{
public static class MonoBehaviourExt
{
public static void TestObject(this GameObject obj) {}
public static void TestMono(this MonoBehaviour obj) {}
}
}
在start((方法中,testmono((方法带有红线,并且错误显示"'testMono'名称在当前上下文中不存在。"。
据我了解,在C#中,不可能防止类扩展,我看不到我的错误在哪里。
您应该使用this.TestMono()
使其正常工作。