1. 程式人生 > >unity自動獲取特效的播放時間,並自動刪除特效

unity自動獲取特效的播放時間,並自動刪除特效

public float time = 0f;
void Start()
{
    time = ParticleSystemLength();
    Invoke("DestroySelf", time);
    print(ParticleSystemLength());
}

void DestroySelf()
{
    Destroy(this.gameObject);
}
float ParticleSystemLength()
{
    ParticleSystem []particleSystems = this.transform.GetComponentsInChildren<ParticleSystem>();
    float maxDuration = 0;
    foreach(ParticleSystem ps in particleSystems){
        if(ps.enableEmission){
            if(ps.loop){
                return -1f;
            }
            float dunration = 0f;
            if(ps.emissionRate <=0){
                dunration = ps.startDelay + ps.startLifetime;
            }else{
                dunration = ps.startDelay + Mathf.Max(ps.duration,ps.startLifetime);
            }
            if (dunration > maxDuration) {
                maxDuration = dunration;
            }
        }
    }
    return maxDuration;
}