10

Before 5.5 particle system variables could be accessed via ParticleSystem and were read/write. Now they're accessed via ParticleSystem.MainModule and thus a lot of code has become obsolete. The API Updater has not been able to fix most of the issues. I've read through the new documentation but I can't figure out how the new variable types are supposed to be used. For example in JetParticleEffect.cs this line causes a warning:

// set the original properties from the particle system
m_OriginalLifetime = m_System.startLifetime;

The warning states: 'ParticleSystem.startLifetime' is obsolete: 'startLifetime property is deprecated. Use main.startLifetime or main.startLifetimeMultiplier instead.'

I've tried the following:

m_OriginalLifetime = m_System.main.startLifetime;
// error: Cannot implicitly convert type 'UnityEngine.ParticleSystem.MinMaxCurve' to 'float'

I believe the answer has something to do with the minMaxCurve constant variables as this compiles:

m_OriginalLifetime = m_System.main.startLifetime.constant;

But there is almost no explaination in the docs. Can anyone shed some light on this?

Also, where do the new multipliers fit in? I assume where previously you could do this:

particle.startSize *= myMultiplier

... you should now do this?

particle.main.startSizeMultiplier = myMultiplier
3
  • m_OriginalLifetime = (float)m_System.main.startLifetime;
    – jdweng
    Dec 30, 2016 at 9:35
  • I typed a long answer for this question yesterday. Was about to post it and you deleted it. Too lazy to type it again but this problem reminds me of this.
    – Programmer
    Dec 30, 2016 at 10:36
  • 1
    My apologies, I didn't think the question was well worded and on re-reading it I felt that hadn't done enough of my own research to justify asking the community. I've since read all of the the relevant docs and feel my new question is more precise.
    – Absinthe
    Dec 30, 2016 at 11:28

1 Answer 1

21

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.