GameObject Add Remove Find

GameObject¸¦ Ãß°¡ »èÁ¦ °Ë»ö¿¡ ´ëÇؼ­ ¾Ë¾Æº¸ÀÚ.

ÀÚ½Ä °ÔÀÓ¿ÀºêÁ§Æ® Ãß°¡ »èÁ¦

Ãß°¡´Â Parent¸¸ ÁöÁ¤ÇØÁÖ¸é µÈ´Ù.
»èÁ¦´Â Destroy¿¡¼­ GameObject·Î »èÁ¦ ÇÑ´Ù.

using UnityEngine;

public class ScrollController : MonoBehaviour
{
    [SerializeField]
    RectTransform prefab = null;

    void AddItem()
    {
        for (int i = 0; i < 15; i++)
        {
            var item = GameObject.Instantiate(prefab) as RectTransform;
            item.SetParent(transform, false);
        }
    }

    void RemoveItem()
    {
        int count = transform.childCount;
        for (int n = count-1; n >= 1; --n)
        {
            GameObject.Destroy(transform.GetChild(n).gameObject);

            //GameObject.Destroy(transform.GetChild(n));
            //¿¡·¯ ¹ß»ý Can't remove RectTransform because Image (Script) depends on it
        }
        //transform.DetachChildren();  ÀڽĵéÀÌ ·çÆ®¿¡ ³ª¿­µÈ´Ù.
    }
}

È°¼ºÈ­ ³ëµå, ÄÄÆ÷³ÍÆ® °Ë»ö

´ÙÀ½°ú °°ÀÌ È°¼ºÈ­ µÇ´Â ÀÖ´Â ³ëµå¿¡¼­ Content¸¦ °Ë»öÇØ º¸ÀÚ.


¿ÀºêÁ§Æ® °Ë»ö
        GameObject item = GameObject.Find("ScrollView/Content");

Conten¿¡ ºÙ¾î ÀÖ´Â ScrollController.cs ½ºÅ©¸³Æ® ã±â
ÄÚµå À§Ä¡´Â Canvas ¿ÀºêÁ§Æ®ÀÌ´Ù.

ÄÄÆ÷³ÍÆ® °Ë»ö
ScrollController scrollCtrl = this.GetComponentInChildren<ScrollController>();

ºñÈ°¼ºÈ­ ³ëµå, ÄÄÆ÷³ÍÆ® °Ë»ö

ScrollView°¡ ´ÙÀ½°ú °°ÀÌ ºñÈ°¼ºÈ­ µÇ¾î ÀÖÀ»¶§ °Ë»öÇÑ´Ù.



ºñÈ°¼ºÈ­ »óÅ¿¡¼­ GameObject.Find·Î´Â °Ë»öÀÌ ¾ÈµÈ´Ù.

¿ÀºêÁ§Æ® °Ë»ö ( GameObject.Find ´ë½Å »ç¿ëÇÒ¼ö ÀÖ´Ù )
    GameObject obj = FindGameObject(gameObject, "ScrollView/Content");

    static public GameObject FindGameObject(GameObject parent, string name)
    {
        GameObject obj = parent;

        char[] sep = new char[] { '/' };
        string[] words = name.Split(sep);

        for (int n = 0; n < words.Length; ++n)
        {
            obj = SubFindChild(obj, words[n]);
            if (obj == null)
                break;
        };

        return parent;
    }

    static GameObject SubFindChild(GameObject parent, string name)
    {
        Transform[] trans = parent.GetComponentsInChildren<Transform>(true);
        for(int n = 0; n < trans.Length; ++n)
        {
            if (trans[n].name == name)
                return trans[n].gameObject;
        }
        return null;
    }

ÄÄÆ÷³ÍÆ® °Ë»ö
ScrollController scrollCtrl = this.GetComponentInChildren<ScrollController>(true);

¿ÀºêÁ§Æ® °Ë»ö (nameÀÌ gameObjectÀÇ Á÷°è ÀÚ½ÄÀ̾î¾ß ÇÑ´Ù )
    GameObject obj = FindGameObjectByChild(gameObject, "ScrollView/Content");

    static public GameObject FindGameObjectByChild(GameObject parent, string name)
    {
        GameObject obj = parent;

        char[] sep = new char[] { '/' };
        string[] words = name.Split(sep);

        for (int n = 0; n < words.Length; ++n)
        {
            obj = SubFindChildByChild(obj, words[n]);
            if (obj == null)
                break;
        }

        return obj;
    }

    static GameObject SubFindChildByChild(GameObject parent, string name)
    {
        Transform trans = parent.transform;
        for(int n = 0; n < trans.childCount; ++n)
        {
            Transform child = trans.GetChild(n);
            if (child.name == name)
                return child.gameObject;
        }
        return null;
    }