今天遇到了这个问题,最后得出了2种解决方案,经过测试后,销毁所有子物体所用的时间,基本相同
情景:有一个父物体A,有子物体B1.B2.B3......B10等10个子物体
现在想销毁B1----B10,保留A
方法一:找到所有子物体,依次销毁
方法二:给每个子物体添加一个自我销毁的方法,在满足特定条件的情况下发送广播
方法一:
using UnityEngine;
using System.Collections;
/// <summary>
/// 脚本位置:将此脚本挂载到父物体A身上
/// 脚本功能:用于测试销毁所有子物体
/// </summary>
public class Parent : MonoBehaviour
{
void Start ()
{
int childCount = transform.childCount;
for (int i = 0; i < childCount ; i++) {
Destroy (transform.GetChild (0).gameObject);
}
}
}
方法二:
using UnityEngine;
using System.Collections;
/// <summary>
/// 脚本位置:将此脚本挂载到父物体A身上
/// 脚本功能&#