Unity刪除一個GameObject下的所有子項

結構:

Root

    |---- Child A

    |---- Child B

    |---- Child C


需求:

在代碼中讓Unity刪除一個GameObject下的所有子項


思路:

Unity中不能對GameObject對象使用GetComponent

所以可以用Transform來代替


解決:

Transform[] childs = Root.GetComponent<Transform>();

然後對childs[i].gameObject使用destroy等即可

注:記得i=0即是root本身,子項序號從i=1開始,這裏沒有再測試,請自行確認

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章