了解“结构"在Unity ParticleSystem中 [英] Understanding the "Struct" in Unity ParticleSystem

查看:245
本文介绍了了解“结构"在Unity ParticleSystem中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码正在工作,"Particle"是类"ParticleSystem"的一个实例.

The code below is working, "Particle" is an instance of class "ParticleSystem".

"Particle.emission"是一个仅获取属性,返回结构"ParticleSystem.EmissionModule"

"Particle.emission" is a get-only property return struct "ParticleSystem.EmissionModule"

"em.rate"是一个属性,类型为结构"ParticleSystem.MinMaxCurve"

"em.rate" is a property, the type is struct "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

当前,我的猜测是结构"ParticleSystem.EmissionModule"是否存储了一些可以链接或与原始"Particle"实例相关的引用?

Currently, my guess is the struct "ParticleSystem.EmissionModule" stored some references that can link or relate to the original "Particle" instance?

推荐答案

我也注意到了这种现象,但是在深入了解.NET Reflector之后,我发现了正在发生的事情.

I noticed this behavior too but I found out what's happening after digging deeper with .NET Reflector.

具有最新Unity版本的代码的完整示例:

The complete example of your code with the latest Unity version:

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

注意事项:

ParticleSystemclass.

EmissionModulestruct.

要在Unity 5及更高版本中更改粒子的发射速率,必须获取 ,然后将其存储在临时 EmissionModule (结构)中那么您可以修改它的 rate 变量

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

这是如何工作的?

当您这样做 :

When you do:

ParticleSystem particle = GetComponent<ParticleSystem>(); 

或创建/实例化新的ParticleSystem或通过编辑器附加一个,则Unity将创建新的EmissionModule实例. EmissionModule具有一个internal构造函数,该构造函数将ParticleSystem作为参数. Unity将立即将当前ParticleSystem实例传递给此EmissionModule构造函数,并且该实例将存储在EmissionModule结构中的临时变量中,以备后用.

or create/instantiate new ParticleSystem or attach one through the Editor, Unity will create new EmissionModule instance. EmissionModule has a an internal constructor that takes ParticleSystem as a parameter. 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.

它看起来像这样:

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

当您这样做 :

When you do:

ParticleSystem.EmissionModule em = particle.emission;

Unity将根据当前粒子(particle)创建EmissionModule的新实例,并将其返回.它将包含已保存的ParticleSystem(tempParticleSystem)引用.请记住,ParticleSystem是一个类,因此引用仍然存在. emission 属性仅具有get访问器.没有set访问器.因此,它是一个 read 唯一属性.

Unity will create new instance of EmissionModule from the current particle (particle) and return it. That will contain that ParticleSystem (tempParticleSystem) reference that was saved. Remember that ParticleSystem is a class, so the reference is still there. The emission property only have the get accessor. No set accessor. Because of this, it is a read only property.

emission 属性看起来像这样:

The emission property looks something like this:

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

最后,当您这样做 :

Finally, when the you do:

em.rate = ....

或更改发射速率,保存的参考用于更改用C ++编写的Unity本机端的粒子速率.

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++.

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

为简化此操作,我们可以称其为struct(EmissionModule)内class(ParticleSystem)的结果.

To simplify this, we can call this a result of a class (ParticleSystem) inside a struct (EmissionModule).

这篇关于了解“结构"在Unity ParticleSystem中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆