无法扩展 MonoBehaviour class、"The name does not exist in the current context" 错误
Unable to extend MonoBehaviour class, "The name does not exist in the current context" Error
我有一个 class 继承了 MonoBehaviour。 GameObject 的扩展方法有效,但不适用于 MonoBehaviour。
Class 文件
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() 方法带有红线下划线,错误显示 "The name 'TestMono' does not exist in the current context."。
据我了解,在 C# 中无法阻止 class 扩展,我也看不出我的错误在哪里。
你应该使用 this.TestMono()
来让它工作。
我有一个 class 继承了 MonoBehaviour。 GameObject 的扩展方法有效,但不适用于 MonoBehaviour。
Class 文件
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() 方法带有红线下划线,错误显示 "The name 'TestMono' does not exist in the current context."。
据我了解,在 C# 中无法阻止 class 扩展,我也看不出我的错误在哪里。
你应该使用 this.TestMono()
来让它工作。