Skip to content

Instantly share code, notes, and snippets.

@Velro
Created August 4, 2015 06:00
Show Gist options
  • Save Velro/3b10f4de8ba7188602a9 to your computer and use it in GitHub Desktop.
Save Velro/3b10f4de8ba7188602a9 to your computer and use it in GitHub Desktop.
public class EnumeratedDestroy : MonoBehaviour
{
private List<GameObject> shallowList;
private List<List<GameObject>> masterList;
void Start ()
{
shallowList = new List<GameObject>();
masterList = new List<List<GameObject>>();
PopulateLists(transform, 0);
for (int i = masterList.Count - 1; i >= 0; i--)// fill from deepest to shallowest
{
shallowList.AddRange(masterList[i]);
}
StartCoroutine(Delete(shallowList));
}
private void PopulateLists(Transform parent, int depth)
{
for (int i = 0; i < masterList.Count - 1; i++)
{
if (depth > masterList.Count-1)//initialize new lists for each depth
{
List<GameObject> list = new List<GameObject>();
masterList.Add(list);
}
masterList[depth].Add(parent.GetChild(i).gameObject);
//recurse into next layer
if (parent.GetChild(i).childCount != 0)
{
PopulateLists(parent.GetChild(i), depth + 1);
}
}
}
//call once
private IEnumerator Delete(List<GameObject> shallowList)
{
for (int i = 0; i < shallowList.Count; i++ )
{
Destroy(shallowList[i]);
yield return new WaitForSeconds(0);
}
Destroy(this.gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment