以下两段代码等价。
未使用lambda函数的代码:
private Button PB; private void Start() { PB = GameObject.Find("PButton").GetComponent<Button>(); PB.onClick.AddListener(Foo); } public void Foo() { SP("..."); } public void SP(string PN) {}
使用lambda函数的代码:
private Button PB; private void Start() { PB = GameObject.Find("PButton").GetComponent<Button>(); PB.onClick.AddListener(() => SP("...")); } public void SP(string PN) {}
问题地址:
https://forum.unity3d.com/threads/why-wont-onclick-addlistener-accept-a-field.357791/