从实例化按钮中识别按钮单击-将游戏对象隐藏在阵列中

时间:2019-03-05 14:06:16

标签: c# user-interface unity3d button instantiation

代码只是为UI生成按钮,然后单击按钮将游戏对象隐藏在数组中,一次仅可见一个对象。目前,我在努力使按钮工作并更改可见的游戏对象,但设法在数组中获取对象名称以替换按钮文本。请帮忙!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonListControl : MonoBehaviour
{

    [SerializeField]
    private GameObject ButtonTemplate;

    [SerializeField]
    private GameObject[] ApplianceList;
    GameObject ApplianceGUI;
    private int index;

    void Start()
    {
        ApplianceGUI = GameObject.Find("Canvas_GUI_ScrollList");
        ApplianceGUI.SetActive(false);

        // Finds GUI and turns off on run.

        foreach (GameObject Appliance in ApplianceList)
        {
            GameObject Button = Instantiate(ButtonTemplate) as GameObject;
            Button.GetComponent<ButtonListButton>().SetText(Appliance.name);
            Button.transform.SetParent(ButtonTemplate.transform.parent, false);

            Appliance.SetActive(false);

            if (ApplianceList[0])
                ApplianceList[0].SetActive(true);
            Button.SetActive(true);
        }
    }
  public void Option1Clicked()
    {
        foreach (GameObject Appliance in ApplianceList)
        {
            Appliance.SetActive(false);
            //Sets the current Game Object to false 

            ApplianceList[1].SetActive(true);
            Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[1]);
        }
    }
       public void Option2Clicked()
    {
        foreach (GameObject Appliance in ApplianceList)
        {
            Appliance.SetActive(false);
            //Sets the current Game Object to false 

            ApplianceList[2].SetActive(true);
            Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[2]);
        }
    }
       public void Option3Clicked()
    {
        foreach (GameObject Appliance in ApplianceList)
        {
            Appliance.SetActive(false);
            //Sets the current Game Object to false 

            ApplianceList[3].SetActive(true);
            Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[3]);
        }
    }
       public void Option4Clicked()
    {
        foreach (GameObject Appliance in ApplianceList)
        {
            Appliance.SetActive(false);
            //Sets the current Game Object to false 

            ApplianceList[4].SetActive(true);
            Debug.Log("New Appliance Chosen, Hiding Old model and displaying:", ApplianceList[4]);
        }
    }

1 个答案:

答案 0 :(得分:0)

实例化GameObject时,需要从模板进行复制。 调用SetActive时,可以在模板对象上调用它,因此它很可能应用于预制件。

您应该改为在实例化对象上调用它,因此您需要另一个列表来存储每个克隆。

此外,您还可以简化代码,为每个按钮提供一个索引(在实例化时),以表示其在列表中的位置以及对列表本身的引用。

这样,您可以将Inspector中的OnClick回调设置为ButtonListButton自己的方法,而不必为每个方法创建OptionNClicked

public sealed class ButtonListControl : MonoBehaviour
{
    private GameObject[] ApplianceList;

    // Hide every button except the one with the given index
    public void HideAllBut(int index)
    {
        for (int i = 0; i < ApplianceList.Length; i++)
        {
            ApplianceList[i].SetActive(i == index);
        }
    }

}
public sealed class ButtonListButton : MonoBehaviour
{
    [SerializeField]
    public int Index;
    [SerializeField]
    ButtonListControl List;

    // This asks the list to hide every button except for this one
    public void HideOthers()
    {
        List.HideAllBut(Index);
    }
}