[Getting Started with Unity] 17. Script access to parent-child nodes

[Getting Started with Unity] Script access to parent-child nodes

    Hello everyone, I am Lampard~~

    Welcome to the Unity Getting Started series of blogs. The knowledge I have learned comes from Teacher Afa at Station B~Thanks 

(1) Parent node

(1) Visit the parent node

    We are not unfamiliar with the parent-child relationship. In cocos, node:getParent() is often used to obtain the parent, and node:getChild() is used to obtain the child node. How do you call it in unity?

    It is strange in unity, its parent-child relationship is recorded on the transform component (this may also be the reason why transform cannot be deleted?), so we can get the parent node through the following code

Transform parentTransform = this.transform.parent;

    It is worth noting that the parent obtained through Transform is also a Transform type component . If I want to access the object of the parent node, I need to add one more step:

Transform parentTransform = this.transform.parent;
GameObject parent = parentTransform.gameObject;

(2) Modify the parent node

    We can access the parent node, and modify the parent-child relationship of objects through tranform.SetParent . For example, the current parent-child relationship is as follows:

    We create a new ParentLogic script to mount on the BBB object

    First print the name of the parent class in start, then monitor the mouse click action in update, and switch the parent class of the BBB node to CCC when clicking, the code is as follows:

    void Start()
    {
        Transform parentTransform = this.transform.parent;
        GameObject parent = parentTransform.gameObject;
        Debug.Log("父类的名称是:" + parent.name);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GameObject newParent = GameObject.Find("CCC");
            this.transform.SetParent(newParent.transform);
        }
    }

    It can be seen that the name of the parent class is AAA at the beginning, and after clicking, the parent class becomes CCC

(2) Child nodes

(1) Traverse all child nodes foreach

    If we want to traverse all child nodes, we can do it through the foreach method . Its syntax is as follows:

foreach(Transform child in transform)
{
    // DO SOMETHING
}

    Among them, child is all the child transform components under the transform component, so we can access the names of all child nodes through such code

foreach(Transform child in transform)
{
    GameObject childObj = child.gameObject;
    Debug.Log("子物体名称为:" + childObj.name)
}

    For example, there are BBB, CCC, DDD, and sub-nodes under the AAA node, and the DDD sub-node has a grandchild node of EEE. Let's see how the printing effect is.

     code show as below:

    void Start()
    {
        GameObject AAA = GameObject.Find("AAA");
        foreach (Transform child in AAA.transform)
        {
            GameObject childObj = child.gameObject;
            Debug.Log("子物体名称为:" + childObj.name);
        }
    }

     It can be seen that the printed result prints out all three child nodes, but the grandchild node EEE does not , so if we want to get one of the child nodes, we can add an If name == XXX when foreach judgment

(2) Directly and accurately find a certain child node

    Previously, we did not recommend using GameObject.Find to find objects. On the one hand, it is easy to duplicate the name, and on the other hand, it is more expensive. If we want to accurately find a certain child node under the node, can we only use foreach to find it?

   Unity's transform component provides us with a Find method , which, like GameObject's FInd, needs to pass in the name or path of the object. The advantage is that it accurately searches among its child nodes, so that both loss and accuracy are important. much stronger

    void Start()
    {
        GameObject AAA = GameObject.Find("AAA");
        Transform BBB =  AAA.transform.Find("BBB");
        Debug.Log(BBB.gameObject.name);
    }

Alright, that's all for today, thank you all for reading! ! !
Like and follow! ! !

Guess you like

Origin blog.csdn.net/cooclc/article/details/130307994