思路就是:
有一个对象池,管理各种特效。
当需要播放特效时,触发如下代码:
blocker为粒子生成的位置
var particles = gamePools.iceParticlesPool.GetObject();
if (particles != null)
{particles.transform.position = blocker.transform.position;particles.GetComponent<TileParticles>().fragmentParticles.Play();
}
Prefab有两个脚本:
1. AutoKillPooled(用途是到了时间就自杀)
public class AutoKillPooled : MonoBehaviour{public float time = 2.0f;private PooledObject pooledObject;private float accTime;/// <summary>/// Unity's OnEnable method./// </summary>private void OnEnable(){accTime = 0.0f;}/// <summary>/// Unity's Start method./// </summary>private void Start(){pooledObject = GetComponent<PooledObject>();}/// <summary>/// Unity's Update method./// </summary>private void Update(){accTime += Time.deltaTime;if (accTime >= time){pooledObject.pool.ReturnObject(gameObject);}}}
2. TileParticles(播放粒子)
TileParticles的脚本附在这个prefab上面,脚本内容如下:
/// <summary>/// This class identifies the particles emitted when a tile entity is destroyed./// </summary>public class TileParticles : MonoBehaviour{public ParticleSystem fragmentParticles;/// <summary>/// Unity's Awake method./// </summary>private void Awake(){Assert.IsNotNull(fragmentParticles);}}
Fragement Particles为该prefab的子物体,上面有particles system组件。
因为particles system配置较复杂:这里直接上传prefab了
prefab下载地址
粒子prefabunity资源-CSDN文库