[Unity] Use arrays and linked lists to control multiple buttons and corresponding events

Encountered a situation where you need to use multiple buttons to control the display and hiding of multiple objects. The idea is as follows:

First, get all the buttons, add them to the linked list, and add all the objects to another linked list, and then control them by name (the button corresponds to the name of the object).

I encountered several problems in the middle, one of which was that the object instance could not be loaded. PS: It turns out that I forgot to give the linked list New, such basic common sense is a bit pitted.

 private void GetUI()
    {
        Btn_Shebei1 = ShebeiPanel.transform.Find("Btn_Shebei1").gameObject;//按钮组1
        Btn_Shebei2 = ShebeiPanel.transform.Find("Btn_Shebei2").gameObject;//按钮组2
        Btn_Shebei3 = ShebeiPanel.transform.Find("Btn_Shebei3").gameObject;//按钮组3

        PanelImg = ShebeiPanel.transform.Find("PanelImg").gameObject;//物体组1

        Btn_Objs = new List<GameObject>();//按钮列表
        Img_objs = new List<GameObject>();//物体列表

        GetObj(Btn_Objs,Btn_Shebei1);
        GetObj(Btn_Objs, Btn_Shebei2);
        GetObj(Btn_Objs, Btn_Shebei3);

    }

    /// <summary>
    /// 把一个物体下的所有子物体添加进链表中
    /// </summary>
    /// <param name="objs">链表</param>
    /// <param name="obj">物体</param>
    private void GetObj(List<GameObject> objs,GameObject obj)
    {
        for (int i = 0; i < obj.transform.childCount; i++)
        {
            objs.Add(obj.transform.GetChild(i).gameObject);
            Debug.Log(obj.transform.GetChild(i).name);
        }
    }

Follow up questions:

Button group 2 and button group 3 are triggered by a button in button group 1, and display objects at the same time. This part needs to be solved, so I will write it here first, and continue to solve this problem later.

follow-up to the above question.

It was a very simple question, but it made me think about it for a long time, and I am a little ashamed of my years of experience.

Paste the follow-up content script:

 /// <summary>
    /// 所有按钮与物体对应的控制
    /// </summary>
    private void AddBtn()
    {
        foreach (GameObject btn_item in Btn_Objs)
        {
            btn_item.GetComponent<Button>().onClick.AddListener(() => {
                foreach (GameObject img_obj in Img_objs)
                {
                    if (img_obj.name == btn_item.name)//如果按钮和物体名字相同,则物体显示,否则物体消失
                    {
                        img_obj.SetActive(true);//如果名字相同的物体激活
                        Btn_Cro(btn_item);
                    }
                    else
                    {
                        //让所有的图片都消失
                        img_obj.SetActive(false);
                    }
                }
            });
        }
    }
    /// <summary>
    /// 针对按钮组2,按钮组3的控制
    /// </summary>
    /// <param name="btn_item"></param>
    private void Btn_Cro(GameObject btn_item)
    {
        //如果点击的名字是前处理按钮,或者是提升机、除梗破碎机按钮,则激活按钮组2  否则按钮组2消失
        if (btn_item.name == "QianChuli" || btn_item.name == "Tishengji" || btn_item.name == "Chugeng")
        {
            Btn_Shebei2.SetActive(true);
        }
        else
        {
            Btn_Shebei2.SetActive(false);
        }

        //如果点击的名字是生产线按钮,或者是在按钮组3里面的子按钮,则激活按钮组3  否则按钮组3消失
        if (btn_item.name == "Shengchanxian" || btn_item.transform.parent.name == "Btn_Shebei3")//按下生产线按钮 按钮数组3显示控制
        {
            Btn_Shebei3.SetActive(true);
        }
        else
        {
            Btn_Shebei3.SetActive(false);
        }
    }

Simply put the idea:

1. Get all buttons and all pictures.

2. The name of the setting button is the same as that of the picture.

3. Add an event response to the button, and if the button name is the same, the picture will be displayed.

4. In the previous step, add a method to solve the control of button group 2 and button group 3

5. Button group 2 and button group 3 need specific settings, as shown in the code

Guess you like

Origin blog.csdn.net/CSDN_6954/article/details/124796512