理解“结构"在 Unity 粒子系统中 [英] Understanding the "Struct" in 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" 是一个属性,类型是 struct "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?
注意struct不是引用,不能直接修改,否则会导致CS1612
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);
注意事项:
ParticleSystem
是一个 class
.
EmissionModule
是一个 struct
.
要在 Unity 5 及更高版本中更改粒子的发射率,您必须获得 ParticleSystem.emission
然后将其存储在临时 EmissionModule
(struct) 然后你可以修改它的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
这是如何工作的?
当你这样做时:
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;
}
当你这样做时:
ParticleSystem.EmissionModule em = particle.emission;
Unity 将从当前粒子 (particle
) 创建 EmissionModule
的新实例并返回它.这将包含已保存的 ParticleSystem
(tempParticleSystem) 引用.记住 ParticleSystem
是一个类,所以引用仍然存在.emission
属性只有 get
访问器.没有 set
访问器.因此,它是一个只读属性.
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);
}
}
最后,当你这样做时:
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代码>).
To simplify this, we can call this a result of a class
(ParticleSystem
) inside a struct
(EmissionModule
).
这篇关于理解“结构"在 Unity 粒子系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!