2014-07-23 50 views
0

我获得了代码的这个snippit,它实例化了一个粒子系统预制。我遇到的问题是克隆在5秒延迟后不会被破坏。任何建议表示赞赏。Unity C#4.5.2 2D销毁实例化的粒子系统预制

private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position) 
{ 
    ParticleSystem newParticleSystem = Instantiate(
     prefab, 
     position, 
     Quaternion.identity 
     ) as ParticleSystem; 

    if(newParticleSystem.gameObject != null) 
    { 
     Destroy(
      newParticleSystem.gameObject, 
      newParticleSystem.startLifetime 
      ); 
    } 

    return newParticleSystem; 
} 

回答

1

您的代码依赖于称为ParticleSystem的任何东西来跟踪何时销毁系统。我会做的是这样的:

private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position) 
{ 
    ParticleSystem newParticleSystem = Instantiate(
     prefab, 
     position, 
     Quaternion.identity 
     ) as ParticleSystem; 

    newParticalSystem.AddComponent<TimedDestroy>().delay = newParticleSystem.startLifetime; 

    return newParticleSystem; 
} 

,然后添加这个脚本到您的项目:

using UnityEngine; 
public class TimedDestroy : MonoBehaviour 
{ 
    public float delay; 

    void Start() 
    { 
     Invoke("destruct",delay); 
    } 

    public void destruct() 
    { 
     Destroy(gameObject); 
    } 
}