Unity3d 移除某個組件下的 子對象(列表更新)
對ScrollView 列表內容更新時,通常需要先清空列表,再讀取數據,而清空列表實際就是銷燬該列表上的item,來實現清空列表的目的
實現如下:
//常用於,刪除清空列表(更新UI時)
private void RemoveAllListViewItem() {
GameObject obj= transform.FindChild("Scroll View/Viewport/Content").gameObject; // 當前組件的列表內容對象
foreach (Transform child in obj.transform) {
GameObject.Destroy(child.gameObject);
}
}
2018/5/10 更新:
對於 scroll view 裏 content 組件的 清除子組件,有個更好的方法是直接調用 該組件的消除方法:
// 讓conten 下的子組件脫離
scroll.content.DetachChildren();