In 5.3, ParticleSystem.emissionRate is deprecated in favour of ParticleSystem.emission, but the docs don’t mention what part of emission… I see a mode which can be set to ‘constant’, and a constantMin and constantMax on MinMaxCurve… do I need to set this mode, and set min and max to be the same value, to get the old behaviour of setting emissionRate to one value?
+1 , emissionRate property is deprecated. Use emission.rate instead. but HOW.
+1. The problem is that all of the properties are either struct which cannot be edited inline or readonly!
This should help:
Still don’t know to get rate value for example. When I’m going set rate value I have to use: new ParticleSystem.MinMaxCurve(0); everytime I want to change rate value
The values in the module can not be set. e.g. GetComponent().emission.rate = new ParticleSystem.MinMaxCurve(100); doesn’t work.
Edit: Can be set when assign the module to a local variable, it works but kind of a pain.
That the way unity apparently want us to do it.
Just get a copy of the struct and modify it , unity has it linked in the backend and will update the data back to the particle system without you needing to write all the data back what could be very slow if you would like to do that every frame.
This is just so much work!
var em = Particle.emission;
var rate = new ParticleSystem.MinMaxCurve();
rate.constantMax = emissionRate;
em.rate = rate;
Hmm, bit of a faff, but that’ll work - thanks! Unity people: this is the kind of thing that would be helpful in the docs when you deprecate things.
I set the constantMin too, just in case.
Looks like it’s time to code some extension methods to make things simpler…
public static class ParticleSystemExtension
{
public static void EnableEmission(this ParticleSystem particleSystem, bool enabled)
{
var emission = particleSystem.emission;
emission.enabled = enabled;
}
public static float GetEmissionRate(this ParticleSystem particleSystem)
{
return particleSystem.emission.rate.constantMax;
}
public static void SetEmissionRate(this ParticleSystem particleSystem, float emissionRate)
{
var emission = particleSystem.emission;
var rate = emission.rate;
rate.constantMax = emissionRate;
emission.rate = rate;
}
}
Thanks for the script, this really saves some time.
Thanks for the extension script.
Hm I’m not sure I understand. The EmissionModule is (titled as) a struct ( value-type ), but I can reference it and then use it to edit values? Does the struct store a reference back to the particlesystem? Does this also work if I make a new EmissionModule and assign it to a particlesystem?
What black magic is going on here?
I would also like an explanation. As a read-only struct (not a class), it would seem to be actually impossible to modify the EmissionModule of a ParticleSystem?
In particular, when doing:
var myEmission = particleSystem.emission;
in C#, the variable myEmission
there is a copy of the particleSystem’s emission
, not a reference! And therefore, any changes to this myEmission
variable would not affect the particleSystem at all?
And yet, modifying this copy is what the ParticleSystem.Emission docs shows as an example. So… what’s going on here?
Edit: I see a Unity dev offhandedly said in another thread that yes, modifying the copied struct is the intended thing to do, even though it doesn’t make much intuitive sense. I would like to highlight how opaque this behavior is to developers.
Why god why???
Thelo link explain. A little.
SO in my case, to dynamic change emission ( and always enable it )
public ParticleSystem turbina1;
private ParticleSystem.EmissionModule EmissionTurbina1;
void Start () {
EmissionTurbina1 = turbina1.emission;
}
void Update () {
EmissionTurbina1.enabled = true;
EmissionTurbina1.rate = new ParticleSystem.MinMaxCurve(some_number);
}
Yes, I’m interested in how they even got this working and secondly, why the hell are we going against everything OOP and C# every said?
Could it somehow be talking to the c++ layer where structs are reference type? Would be nice to know.
Well it is running in C++ but it also has to be communicating through a C# layer. So they’re converting a C# struct into a C++ struct(reference)? Not sure why though.
How I can change the rate value in Update?