我正在实例化许多按钮,当点击时需要调用一个函数(通过监听器).但我也经常摧毁它们.这些听众是否也被销毁或者我是否需要删除它们?
例:
public void makeButton()
{
GameObject spawnedButton = Instantiate(prefabButton, prefabButton.transform.position, prefabButton.transform.rotation) as GameObject;
spawnedButton.GetComponent<Button>().onClick.AddListener(()=>
{
listedButtonClicked(someOtherObjectThatWillNotBeDeleted, spawnedButton);
});
}
public void listedButtonClicked(GameObject target, GameObject button)
{
Debug.Log(target);
Debug.Log(button);
}
Run Code Online (Sandbox Code Playgroud)
......所以什么时候spawnedButton被摧毁,这个听众会留下来吗?我正在实例化并删除大量按钮,因此它可能与我相关.