gpt4 book ai didi

c# - 由 ScrollView 和按钮组成的 Unity3D 列表 - 如何删除项目?

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:31 25 4
gpt4 key购买 nike

我已经在 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
}
}

如何检测点击了哪个item的按钮移除了item?

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

List made with ScrollView, Texts and Buttons

最佳答案

只需添加一行代码:

        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
}

关于c# - 由 ScrollView 和按钮组成的 Unity3D 列表 - 如何删除项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38104207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com