1

We have that code from Unity documentation for particle system:

public class ExampleClass : MonoBehaviour {
    void Start() {
        ParticleSystem ps = GetComponent<ParticleSystem>();
        var em = ps.emission;
        em.enabled = true;

        em.type = ParticleSystemEmissionType.Time;

        em.SetBursts(
            new ParticleSystem.Burst[]{
                new ParticleSystem.Burst(2.0f, 100),
                new ParticleSystem.Burst(4.0f, 100)
            });
    }
}

1) Is var means ParticleSystem.EmissionModule ?
2) How em affect ps.emission without writing ps.emission = em; ?

Mr Zero
  • 163
  • 4
  • 19

2 Answers2

2

var is just a way to let the compiler figure out the Type. You could also write it out in full if you want. You can check this by placing your cursor on var. You should see ParticleSystem.EmissionModule as the inferred Type.

  1. ParticleSystem.EmissionModule provides access to your particle system emission module so that you can manage its properties.

The documentation reads:

Access the particle system emission module.

Particle system modules do not need to be reassigned back to the system; they are interfaces and not independent objects.

bolkay
  • 1,881
  • 9
  • 20
  • thanks for the clarification of the first question , the second one i didn't get it yet ;is EmissionModule an interface ? if yes i thought we can't create objects from interfaces directly if no what is it and how it works ?? – Mr Zero Dec 10 '18 at 03:05
  • 1
    You can. What you can't do is to create an instance of an `Interface`. The same goes for classes marked as `abstract`. If I have an `Istudent` interface, and a `Student` class that implements that interface, I can create a student object by doing: `Istudent student=new Student();`. I hope this helps. – bolkay Dec 10 '18 at 03:09
  • thank you that's true instance is not same as object they are different things , last question how EmissionModule instance em will modify ps.emission attributes like in 'em.enabled = true;' how it will get into ps.emission.enabled without using ps.emission = em; line? – Mr Zero Dec 10 '18 at 03:32
  • As explained in the documentation, `ParticleSystem.EmissionModule` is just a way to set some of the emission properties of your particle system. You can't set `ps.emission=em` or anything like that. – bolkay Dec 10 '18 at 03:39
0

Here's a related question

  1. Yes.
  2. Simply put, this struct ParticleSystem.EmissionModule actually holds a reference to the ParticleSystem. And the getters and setters of its properties are in fact reading and writing the ParticleSystem object, so Unity calls it an "Interface".

But it is actually a struct and not an interface in C#. This behavior violates the immutability of struct and is terrible code.

Karson Jo
  • 71
  • 1
  • 2