【问题标题】:Unity3D list made of scrollview and buttons - how to delete item?由滚动视图和按钮组成的 Unity3D 列表 - 如何删除项目?
【发布时间】:2016-06-29 15:37:29
【问题描述】:

我在 Unity3D 中使用 ScrollView、文本和按钮制作了列表。 当用户单击项目附近的按钮时 - 该项目应被删除。 按钮和文本是使用 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?

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

【问题讨论】:

  • 您是如何创建按钮和文本的,您是使用 OnGui 还是使用拖入场景视图的 Gui 对象?
  • 除非您包含您的代码,否则完全无法提供帮助。请注意,如果您使用的是古老的“onGui”系统,您不能这样做。它已被弃用,不再有效。
  • 我正在使用新的 GUI 系统。我有带有 ScrollView 的画布。文本和按钮是使用实例化创建的。我不知道如何识别 ex 的按钮。第二个按钮 == 第二个项目。
  • 销毁(你的游戏对象)

标签: c# unity3d unity5 unity3d-gui


【解决方案1】:

只需添加一行代码:

        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
        }

【讨论】:

  • 谢谢@JoeBlow,你让我感到自豪;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
相关资源
最近更新 更多