天天看點

調用某物體的子物體和子物體孫物體

調用某物體的子物體和子物體孫物體

public class UI_RingtButton : MonoBehaviour {

    public GameObject ZheZhao; 

    public GameObject Right_Canvas;

    public void JiChu_Button()

    {

        foreach(Transform child in Right_Canvas.GetComponentsInChildren<Transform>(true))    //調用子物體和孫物體

        {

            if (child.name == "jichu")

            {

                if (child.gameObject.activeSelf == true)

                {

                    child.gameObject.SetActive(false);

                }

                else

                {

                    child.gameObject.SetActive(true);

                }

            }

            if (child.name == "zuta")

            {

                child.gameObject.SetActive(true);

            }

            if (child.gameObject.name == "jiaxian")

            {

                child.gameObject.SetActive(true);

            }

        }

        foreach(Transform child in ZheZhao.GetComponent<Transform>())   //調用子物體

        {

            if(child.name== "JiChuMuLu")

            {

                if (child.gameObject.activeSelf == false)

                {

                    child.gameObject.SetActive(true);

                }

                else

                {

                    child.gameObject.SetActive(false);

                }

            }

            else

            {

                child.gameObject.SetActive(false);

            }

        }

    }