Unity C#4.5.2 2D销毁实例化粒子系统预制
我获得了这段代码片段,它实例化了一个粒子系统预置。我遇到的问题是克隆在5秒延迟后不会被摧毁。任何建议都将不胜感激Unity C#4.5.2 2D销毁实例化粒子系统预制,c#,unity3d,2d,destroy,particles,C#,Unity3d,2d,Destroy,Particles,我获得了这段代码片段,它实例化了一个粒子系统预置。我遇到的问题是克隆在5秒延迟后不会被摧毁。任何建议都将不胜感激 private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position) { ParticleSystem newParticleSystem = Instantiate( prefab, position, Quaternion.identity
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;
}
您的代码依赖于所谓的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);
}
}