IsAlive
U3D的粒子系统脚本接口相信很多人都用过,ParticleSyetem类的一系列接口都有一个bool类型的参数——withChildren,通过这个参数可以直接将相同的判断或者操作应用到一整个通过Transform父子关系树关联起来的ParticleSystem实例集合上。然而,但凡方便的功能,里面就必然有性能陷阱……
以IsAlive这个接口为例(用来判断粒子系统是否所有粒子都已经消亡,一般用在非loop的例子发射器上),看看U3D里是如何实现这个接口的:
public bool IsAlive()
{
bool withChildren = true;
return this.IsAlive(withChildren);
}
public bool IsAlive(bool withChildren)
{
if (withChildren)
{
ParticleSystem[] particleSystems = ParticleSystem.GetParticleSystems(this);
ParticleSystem[] array = particleSystems;
for (int i = 0; i < array.Length; i++)
{
ParticleSystem particleSystem = array[i];
if (particleSystem.Internal_IsAlive())
{
return true;
}
}
return false;
}
return this.Internal_Is