Unity C#4.5.2 2D破坏实例化粒子系统预制

时间:2014-07-23 17:30:00

标签: c# unity3d 2d destroy particles

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

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 个答案:

答案 0 :(得分: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);
    }
}