Unity Notes调制粒子系统的颗粒的最大数目

Unity该粒子系统是很容易使用。这样的问题是在实际的过程中遇到的:以控制的粒子系统组件的动态需要可产生颗粒的最大数目。

看doc他说,有maxParticles控制。却没有这个开放的參数。仅仅能通过其他的方式来实现。

这里能够通过手动产生粒子的方式来实现。也即ParticleSystem中的Emit方法,详细代码例如以下:

public class ParticleSystemComp : MonoBehaviour
{
	ParticleSystem mParticleSystem = null;
	int mMaxParticles = 0;

	public int maxParticles
	{	
		get
		{
			return mMaxParticles;
		}
		set
		{
			mMaxParticles = value;
		}
	}

	void Awake()
	{
		mParticleSystem = this.gameObject.GetComponent<ParticleSystem>();
		mParticleSystem.emissionRate = 0.0f;
		mMaxParticles = mParticleSystem.particleCount;
		mParticleSystem.Clear();
	}

	void Update()
	{
		if (mParticleSystem != null)
		{
			if (mParticleSystem.particleCount < mMaxParticles)
			{
				mParticleSystem.Emit(mMaxParticles - mParticleSystem.particleCount);
			}
		}
	}
}

实现过程是对于每一个ParticleSystem加入一个额外的组件,通过该组件来原始PS的更新。

当中的更新原则是推断当前活动的粒子数量,假设粒子数小于指定的最大值的话就再又一次生成几个新的粒子出来进行填充。

此外。出于性能考虑。当中Update内部的操作也能够移至FixedUpdate中进行以降低更新次数。但它并没有带来太大的区别在视觉上。

版权声明:本文博主原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4758803.html