Unity 5.5 obsolete particle system code

particle.startLifetime:

First of all, what Unity did in Unity 5.5 was to add new futures to the ParticleSystem. They also exposed some ParticleSystem API that was hidden before.

ParticleSystem.MainModule.startLifetime is now a type of MinMaxCurve instead of float like ParticleSystem.startLifetime.

By doing this, you are now given more options such as modifying the 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 via code.

enter image description here

The default value of ParticleSystem.MainModule.startLifetime.mode is ParticleSystemCurveMode.Constant

So your m_OriginalLifetime = m_System.main.startLifetime.constant; is fine.

If startLifetime is dynamically or randomly changed to another mode during run-time, then you will have 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 thing apply to particle.startSize.
The particle.startSize property is now m_System.main.startSize;

Although you can’t do m_System.main.startSize.constant *= myMultiplier; because your old code was particle.startSize *= myMultiplier.

You need to get m_System.main.startSize, modify it then assign the modified m_System.main.startSize back 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 are particle.main.startSizeMultiplier and particle.main.startSize used for?

This two variables can also be used to change startLifetime and startSize. It’s main advantage is that it is very efficient. It does not not require that you make a copy of MinMaxCurve like we did above, in order 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 constant. This will to change the overall lifetime multiplier or the the overall size multiplier efficiently.


Changing Color and Color Modes

Color:

There is an implicit operator that lets you use:

ParticleSystem.MainModule main = trailPartical.main;
main.startColor = Color.red;

but startColor is not actually type of Color. The startColor variable is now a type of ParticleSystem.MinMaxGradient.

This is how you should be changing the particle startColor:

//Create Color
ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient();
color.mode = ParticleSystemGradientMode.Color;
color.color = Color.red;

//Assign the color to your particle
ParticleSystem.MainModule main = trailPartical.main;
main.startColor = color;

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 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 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;
}

Leave a Comment