思路就是:

有一个对象池,管理各种特效。

当需要播放特效时,触发如下代码:

blocker为粒子生成的位置

var particles = gamePools.iceParticlesPool.GetObject();if (particles != null){ particles.transform.position = blocker.transform.position; particles.GetComponent().fragmentParticles.Play();}

Prefab有两个脚本:

1. AutoKillPooled(用途是到了时间就自杀)

public class AutoKillPooled : MonoBehaviour{public float time = 2.0f;private PooledObject pooledObject;private float accTime;/// /// Unity's OnEnable method./// private void OnEnable(){accTime = 0.0f;}/// /// Unity's Start method./// private void Start(){pooledObject = GetComponent();}/// /// Unity's Update method./// private void Update(){accTime += Time.deltaTime;if (accTime >= time){pooledObject.pool.ReturnObject(gameObject);}}}

2. TileParticles(播放粒子)

TileParticles的脚本附在这个prefab上面,脚本内容如下:

/// /// This class identifies the particles emitted when a tile entity is destroyed./// public class TileParticles : MonoBehaviour{public ParticleSystem fragmentParticles;/// /// Unity's Awake method./// private void Awake(){Assert.IsNotNull(fragmentParticles);}}

Fragement Particles为该prefab的子物体,上面有particles system组件。

因为particles system配置较复杂:这里直接上传prefab了

prefab下载地址

粒子prefabunity资源-CSDN文库