particle.startLifetime:
First of all, what Unity did in Unity 5.5 was to add new futures to ParticleSystem
. They also discovered some API ParticleSystem
, which was hidden earlier.
ParticleSystem.MainModule.startLifetime
now a MinMaxCurve
type instead of a float like ParticleSystem.startLifetime
.
Thus, now you are given more options, such as changing startLifetime
as a curve.
Reading or writing to ParticleSystem.MainModule.startLifetime
depends on the value of ParticleSystem.MainModule.startLifetime.mode
, which is set through the Editor or through code.
The default value of ParticleSystem.MainModule.startLifetime.mode
is ParticleSystemCurveMode.Constant
So your m_OriginalLifetime = m_System.main.startLifetime.constant;
in order.
If startLifetime
dynamically or accidentally changed to another mode at runtime, you will need to do something like this:
ParticleSystem m_System = GetComponent<ParticleSystem>(); ParticleSystem.MainModule main = m_System.main; ParticleSystem.MinMaxCurve minMaxCurve = main.startLifetime; if (minMaxCurve.mode == ParticleSystemCurveMode.Constant) { m_OriginalLifetime = m_System.main.startLifetime.constant; } else if (minMaxCurve.mode == ParticleSystemCurveMode.Curve) { AnimationCurve animCurveLifetime = m_System.main.startLifetime.curve; } ...
particle.startSize:
The same goes for particle.startSize
. Now the particle.startSize
property m_System.main.startSize;
Although it can not do m_System.main.startSize.constant *= myMultiplier;
because your old code was particle.startSize *= myMultiplier
.
You need to get m_System.main.startSize
, change it, and then change the changed m_System.main.startSize
to m_System.main.startSize
.
particle.startSize *= myMultiplier
should be:
ParticleSystem m_System = GetComponent<ParticleSystem>(); ParticleSystem.MainModule main = m_System.main; ParticleSystem.MinMaxCurve minMaxCurve = main.startSize; //Get Size minMaxCurve.constant *= myMultiplier; //Modify Size main.startSize = minMaxCurve; //Assign the modified startSize back
Then, what particle.main.startSizeMultiplier
and particle.main.startSize
are used for?
These two variables can also be used to change startLifetime
and startSize
. The main advantage is that it is very effective. This does not require you to make a copy of MinMaxCurve
, as we did above, to change startSize
or startSizeMultiplier
.
ParticleSystem m_System = GetComponent<ParticleSystem>(); ParticleSystem.MainModule main = m_System.main; main.startSizeMultiplier = 5;
and
ParticleSystem m_System = GetComponent<ParticleSystem>(); ParticleSystem.MainModule main = m_System.main; main.startLifetimeMultiplier = 8;
Use them if your ParticleSystem.MainModule.startLifetime.mode
is persistent. This will allow you to effectively change the common factor of life expectancy or the common factor.
Change color and color modes
Color
There is an implicit statement that allows you to use:
ParticleSystem.MainModule main = trailPartical.main; main.startColor = Color.red;
but startColor
is not really a Color
type. The startColor
variable startColor
now a type of ParticleSystem.MinMaxGradient
.
Here's how you should change the startColor
particle:
Gradient :
public ParticleSystem particleSystem; void Start() { //Create Gradient key GradientColorKey[] gradientColorKey; gradientColorKey = new GradientColorKey[3]; gradientColorKey[0].color = Color.red; gradientColorKey[0].time = 0f; gradientColorKey[1].color = Color.blue; gradientColorKey[1].time = 0.5f; gradientColorKey[2].color = Color.green; gradientColorKey[2].time = 1f; //Create Gradient alpha GradientAlphaKey[] gradientAlphaKey; gradientAlphaKey = new GradientAlphaKey[3]; gradientAlphaKey[0].alpha = 1.0f; gradientAlphaKey[0].time = 0.0f; gradientAlphaKey[1].alpha = 0.5f; gradientAlphaKey[1].time = 0.5f; gradientAlphaKey[2].alpha = 1f; gradientAlphaKey[2].time = 1f; //Create Gradient Gradient gradient = new Gradient(); gradient.SetKeys(gradientColorKey, gradientAlphaKey); //Create Color from Gradient ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient(); color.mode = ParticleSystemGradientMode.Gradient; color.gradient = gradient; //Assign the color to particle ParticleSystem.MainModule main = particleSystem.main; main.startColor = color; }
Random distance between two colors :
//Create Color from Gradient ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient(); color.mode = ParticleSystemGradientMode.TwoColors; color.colorMin = Color.red; color.colorMax = Color.green; //Assign the color to the particle ParticleSystem.MainModule main = particleSystem.main; main.startColor = color;
Random distance between two gradients :
public ParticleSystem particleSystem; void Start() { //Create Gradient key Min GradientColorKey[] gradientColorKeyMin; gradientColorKeyMin = new GradientColorKey[3]; gradientColorKeyMin[0].color = Color.red; gradientColorKeyMin[0].time = 0f; gradientColorKeyMin[1].color = Color.blue; gradientColorKeyMin[1].time = 0.5f; gradientColorKeyMin[2].color = Color.green; gradientColorKeyMin[2].time = 1f; //Create Gradient alpha Min GradientAlphaKey[] gradientAlphaKeyMin; gradientAlphaKeyMin = new GradientAlphaKey[3]; gradientAlphaKeyMin[0].alpha = 1.0f; gradientAlphaKeyMin[0].time = 0.0f; gradientAlphaKeyMin[1].alpha = 0.5f; gradientAlphaKeyMin[1].time = 0.5f; gradientAlphaKeyMin[2].alpha = 1f; gradientAlphaKeyMin[2].time = 1f; //Create Gradient key Max GradientColorKey[] gradientColorKeyMax; gradientColorKeyMax = new GradientColorKey[3]; gradientColorKeyMax[0].color = Color.red; gradientColorKeyMax[0].time = 0f; gradientColorKeyMax[1].color = Color.blue; gradientColorKeyMax[1].time = 0.5f; gradientColorKeyMax[2].color = Color.green; gradientColorKeyMax[2].time = 1f; //Create Gradient alpha Max GradientAlphaKey[] gradientAlphaKeyMax; gradientAlphaKeyMax = new GradientAlphaKey[3]; gradientAlphaKeyMax[0].alpha = 1.0f; gradientAlphaKeyMax[0].time = 0.0f; gradientAlphaKeyMax[1].alpha = 0.5f; gradientAlphaKeyMax[1].time = 0.5f; gradientAlphaKeyMax[2].alpha = 1f; gradientAlphaKeyMax[2].time = 1f; //Create Gradient Min Gradient gradientMin = new Gradient(); gradientMin.SetKeys(gradientColorKeyMin, gradientAlphaKeyMin); //Create Gradient Max Gradient gradientMax = new Gradient(); gradientMax.SetKeys(gradientColorKeyMax, gradientAlphaKeyMax); //Create Color from Gradient ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient(); color.mode = ParticleSystemGradientMode.TwoGradients; color.gradientMin = gradientMin; color.gradientMax = gradientMax; //Assign the color to the particle ParticleSystem.MainModule main = particleSystem.main; main.startColor = color; }
Random color :
public ParticleSystem particleSystem; void Start() { //Create Gradient key Min GradientColorKey[] gradientColorKeyMin; gradientColorKeyMin = new GradientColorKey[3]; gradientColorKeyMin[0].color = Color.red; gradientColorKeyMin[0].time = 0f; gradientColorKeyMin[1].color = Color.blue; gradientColorKeyMin[1].time = 0.5f; gradientColorKeyMin[2].color = Color.green; gradientColorKeyMin[2].time = 1f; //Create Gradient alpha Min GradientAlphaKey[] gradientAlphaKeyMin; gradientAlphaKeyMin = new GradientAlphaKey[3]; gradientAlphaKeyMin[0].alpha = 1.0f; gradientAlphaKeyMin[0].time = 0.0f; gradientAlphaKeyMin[1].alpha = 0.5f; gradientAlphaKeyMin[1].time = 0.5f; gradientAlphaKeyMin[2].alpha = 1f; gradientAlphaKeyMin[2].time = 1f; //Create Gradient key Max GradientColorKey[] gradientColorKeyMax; gradientColorKeyMax = new GradientColorKey[3]; gradientColorKeyMax[0].color = Color.red; gradientColorKeyMax[0].time = 0f; gradientColorKeyMax[1].color = Color.blue; gradientColorKeyMax[1].time = 0.5f; gradientColorKeyMax[2].color = Color.green; gradientColorKeyMax[2].time = 1f; //Create Gradient alpha Max GradientAlphaKey[] gradientAlphaKeyMax; gradientAlphaKeyMax = new GradientAlphaKey[3]; gradientAlphaKeyMax[0].alpha = 1.0f; gradientAlphaKeyMax[0].time = 0.0f; gradientAlphaKeyMax[1].alpha = 0.5f; gradientAlphaKeyMax[1].time = 0.5f; gradientAlphaKeyMax[2].alpha = 1f; gradientAlphaKeyMax[2].time = 1f; //Create Gradient Min Gradient gradientMin = new Gradient(); gradientMin.SetKeys(gradientColorKeyMin, gradientAlphaKeyMin); //Create Gradient Max Gradient gradientMax = new Gradient(); gradientMax.SetKeys(gradientColorKeyMax, gradientAlphaKeyMax); //Create Color from Gradient ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient(); color.mode = ParticleSystemGradientMode.RandomColor; color.gradientMin = gradientMin; color.gradientMax = gradientMax; //Assign the color to the particle ParticleSystem.MainModule main = particleSystem.main; main.startColor = color; }