由滚动视图和按钮组成的 Unity3D 列表 - 如何删除项目?

Unity3D list made of scrollview and buttons - how to delete item?

我已经在 Unity3D 中使用 ScrollView、Texts 和 Buttons 制作了列表。 当用户单击项目附近的按钮时 - 应删除该项目。 按钮和文本是使用 Instantiate 方法创建的。 项目列表是通用列表(List)。

项目列表:

public List<Item> Items { get; set; }

创建按钮和文本:

public Button itemButton;
public Text itemText;
(...)
public void ShowItems()
{
    ClearItems(); //Destroys button and text gameObjects.

    foreach (var item in Globals.Items)
    {
        var text = Instantiate(itemText) as Text;
        var button = Instantiate(itemButton) as Button;
        button.GetComponentInChildren<Text>().text = "Delete";
        textsList.Add(text); //save Text element to list to have possibility of destroying Text gameObjects
        buttonsList.Add(button);//save Button element to list to have possibility of destroying Button gameObjects
        text.gameObject.SetActive(true);
        button.gameObject.SetActive(true);
        //(...) Setting GUI items position here
    }
}

如何检测点击了哪个项目的按钮删除了项目?

我不知道如何让第二个按钮点击 == 第二项删除。

只需添加一行代码:

        foreach (var item in Globals.Items)
        {
            var text = Instantiate(itemText) as Text;
            var button = Instantiate(itemButton) as Button;
            button.GetComponentInChildren<Text>().text = "Delete";
            textsList.Add(text); //save Text element to list to have possibility of destroying Text gameObjects
            buttonsList.Add(button);//save Button element to list to have possibility of destroying Button gameObjects
            text.gameObject.SetActive(true);
            button.gameObject.SetActive(true);

            // this line:
            button.onClick.AddListener(delegate {Destroy(text.gameObject); Destroy(button.gameObject);});

            //(...) Setting GUI items position here
        }