Unity新版本的粒子代码框架更改过,之前的很多api是不能用的。
新版本的粒子效果把整个数据分成了几大模块,每个模块是一个struct
public struct MainModule
{
public float duration { get; set; }
public float startRotationXMultiplier { get; set; }
public MinMaxCurve startRotationY { get; set; }
public float startRotationYMultiplier { get; set; }.
public MinMaxCurve startRotationZ { get; set; }
public float startRotationZMultiplier { get; set; }
public float flipRotation { get; set; }
public MinMaxGradient startColor { get; set; }
public MinMaxCurve gravityModifier { get; set; }
public float gravityModifierMultiplier { get; set; }
public ParticleSystemSimulationSpace simulationSpace { get; set; }
public Transform customSimulationSpace { get; set; }
public float simulationSpeed { get; set; }
public bool useUnscaledTime { get; set; }
public ParticleSystemScalingMode scalingMode { get; set; }
public bool playOnAwake { get; set; }
public int maxParticles { get; set; }
public ParticleSystemEmitterVelocityMode emitterVelocityMode { get; set; }
public MinMaxCurve startRotationX { get; set; }
public ParticleSystemStopAction stopAction { get; set; }
public float startRotationMultiplier { get; set; }
public bool startRotation3D { get; set; }
public bool loop { get; set; }
public bool prewarm { get; set; }
public float startDelayMultiplier { get; set; }
public MinMaxCurve startLifetime { get; set; }
public float startLifetimeMultiplier { get; set; }
public MinMaxCurve startSpeed { get; set; }
public float startSpeedMultiplier { get; set; }
public bool startSize3D { get; set; }
public MinMaxCurve startSize { get; set; }
public float startSizeMultiplier { get; set; }
public MinMaxCurve startSizeX { get; set; }
public float startSizeXMultiplier { get; set; }
public MinMaxCurve startSizeY { get; set; }
public float startSizeYMultiplier { get; set; }
public MinMaxCurve startSizeZ { get; set; }
public float startSizeZMultiplier { get; set; }
public MinMaxCurve startRotation { get; set; }
[Obsolete("Please use flipRotation instead. (UnityUpgradable) -> UnityEngine.ParticleSystem/MainModule.flipRotation", false)]
public float randomizeRotationDirection { get; set; }
}
这是MainModule的代码,主要是修改粒子修过最上面部分。ParticleSystem中有main字段是保存MainModule数据的。
然后今天看网上说修改方式
ParticleSystem.MainModule main = m_particle.main;
main.duration = setting.duration;
main.loop = false;
当时觉得不可能这样修改,但是测试后确实可以。为什么觉得不可能呢?因为MainModule是值类型,你修改main的值,是不可能影响到ParticleSystem中的main的。
于是就查看了MainModule源码,截取部分:
public struct MainModule
{
private ParticleSystem m_ParticleSystem;
public float duration
{
get
{
return ParticleSystem.MainModule.GetDuration(this.m_ParticleSystem);
}
set
{
ParticleSystem.MainModule.SetDuration(this.m_ParticleSystem, value);
}
}
public bool loop
{
get
{
return ParticleSystem.MainModule.GetLoop(this.m_ParticleSystem);
}
set
{
ParticleSystem.MainModule.SetLoop(this.m_ParticleSystem, value);
}
}
}
看这个应该就明白了,每个Module中应该都保存了ParticleSystem 。然后set方法中,通过ParticleSystem修改的,因为ParticleSystem是引用类型,因此这样修改就应用到ParticleSystem中了
最后补充一句 ParticleSystem.main.loop = fale; 这种写法是不行的,值类型是不允许这样修改的, ParticleSystem.main通过get返回的是值类型,修改是不影响ParticleSystem.main的,因此这种方法被编译禁止,会报错的