简体   繁体   English

了解Unity粒子系统中的“结构”

[英]Understanding the “Struct” in Unity ParticleSystem

The code below is working, "Particle" is an instance of class "ParticleSystem". 下面的代码正在工作,“ Particle”是类“ ParticleSystem”的一个实例。

"Particle.emission" is a get-only property return struct "ParticleSystem.EmissionModule" “ Particle.emission”是一个只读属性返回结构“ ParticleSystem.EmissionModule”

"em.rate" is a property, the type is struct "ParticleSystem.MinMaxCurve" “ em.rate”是一个属性,类型为结构“ ParticleSystem.MinMaxCurve”

ParticleSystem.EmissionModule em = Particle.emission; 
em.rate = new ParticleSystem.MinMaxCurve(5);

My problem is, why the code above can change the rate in "Particle" instance? 我的问题是,为什么上面的代码可以更改“粒子”实例中的速率?

Note the struct is not reference, so it cannot be changed directly, or it will cause CS1612 注意,该结构不是引用,因此无法直接更改,否则将导致CS1612

Currently, my guess is the struct "ParticleSystem.EmissionModule" stored some references that can link or relate to the original "Particle" instance? 当前,我的猜测是结构“ ParticleSystem.EmissionModule”是否存储了一些可以链接或与原始“ Particle”实例相关的引用?

I noticed this behavior too but I found out what's happening after digging deeper with .NET Reflector. 我也注意到了这种行为,但是在深入了解.NET Reflector之后,我发现了正在发生的情况。

The complete example of your code with the latest Unity version: 最新的Unity版本的完整代码示例:

ParticleSystem particle = GetComponent<ParticleSystem>();
ParticleSystem.EmissionModule em = particle.emission;
em.rate = new ParticleSystem.MinMaxCurve(5);

Things to bear in mind: 注意事项:

ParticleSystem is a class . ParticleSystem是一class

EmissionModule is a struct . EmissionModule是一个struct

To change the Particle's emission rate in Unity 5 and above, you must get the ParticleSystem.emission then store it in a temporary EmissionModule (struct) then you can modify it's rate variable 要在Unity 5及更高版本中更改粒子的发射速率,必须获取ParticleSystem.emission然后将其存储在临时的EmissionModule (结构)中,然后可以修改其rate变量

How does this work? 这是如何运作的?

When you do : 当您这样做时

ParticleSystem particle = GetComponent<ParticleSystem>(); 

or create/instantiate new ParticleSystem or attach one through the Editor, Unity will create new EmissionModule instance. 或创建/实例化新的ParticleSystem或通过编辑器附加一个,Unity将创建新的EmissionModule实例。 EmissionModule has a an internal constructor that takes ParticleSystem as a parameter. EmissionModule具有一个internal构造函数,该构造函数将ParticleSystem作为参数。 Unity will immediately pass the current ParticleSystem instance to this EmissionModule constructor and that instance is stored in a temporary variable in the EmissionModule struct for later use. Unity将立即将当前的ParticleSystem实例传递给此EmissionModule构造函数,并且该实例存储在EmissionModule结构的临时变量中,以备后用。

It looks something like this: 看起来像这样:

private ParticleSystem tempParticleSystem;
internal EmissionModule(ParticleSystem particleInstance)
{
    this.tempParticleSystem = particleInstance;
}

When you do : 当您这样做时

ParticleSystem.EmissionModule em = particle.emission;

Unity will create new instance of EmissionModule from the current particle ( particle ) and return it. Unity将根据当前粒子( particle )创建EmissionModule新实例,并将其返回。 That will contain that ParticleSystem (tempParticleSystem) reference that was saved. 它将包含已保存的ParticleSystem (tempParticleSystem)引用。 Remember that ParticleSystem is a class, so the reference is still there. 请记住, ParticleSystem是一个类,因此引用仍然存在。 The emission property only have the get accessor. emission属性仅具有get访问器。 No set accessor. 没有set访问器。 Because of this, it is a read only property. 因此,它是一个只读属性。

The emission property looks something like this: emission属性看起来像这样:

public EmissionModule emission
{
    get
    {
        return new EmissionModule(this);
    }
}

Finally, when the you do : 最后,当您执行以下操作时

em.rate = ....

or change the emission rate, that saved reference is used to change the Particle rate in the native side of Unity which is written in C++. 或更改发射率,该保存的参考将用于更改用C ++编写的Unity本机端的粒子率。

public ParticleSystem.MinMaxCurve rate
{
    get
    {
        ParticleSystem.MinMaxCurve curve = new ParticleSystem.MinMaxCurve();
        getParticleRate(this.tempParticleSystem, ref curve);
        return curve;
    }
    set
    {
        setParticleRate(this.tempParticleSystem, ref value);
    }
}

To simplify this, we can call this a result of a class ( ParticleSystem ) inside a struct ( EmissionModule ). 为了简化此过程,我们可以将其称为structEmissionModule )中的classParticleSystem )的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM