小编Mr *_*ons的帖子

Unity - 当按钮被销毁时onClick的监听器被破坏了吗?

我正在实例化许多按钮,当点击时需要调用一个函数(通过监听器).但我也经常摧毁它们.这些听众是否也被销毁或者我是否需要删除它们?

例:

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被摧毁,这个听众会留下来吗?我正在实例化并删除大量按钮,因此它可能与我相关.

c# unity-game-engine

1
推荐指数
1
解决办法
1320
查看次数

标签 统计

c# ×1

unity-game-engine ×1