Unity 5.5过时的粒子系统代码

时间:2016-12-30 09:31:37

标签: c# unity3d updates deprecated particle-system

5.5之前可以通过ParticleSystem访问粒子系统变量并进行读/写。现在它们是通过ParticleSystem.MainModule访问的,因此很多代码已经过时了。 API Updater无法解决大部分问题。我已经阅读了新文档,但我无法弄清楚应该如何使用新的变量类型。例如,在JetParticleEffect.cs中,此行会引发警告:

// set the original properties from the particle system
m_OriginalLifetime = m_System.startLifetime;

警告声明:'ParticleSystem.startLifetime'已过时:'不推荐使用'startLifetime属性。请改用main.startLifetime或main.startLifetimeMultiplier。'

我尝试了以下内容:

m_OriginalLifetime = m_System.main.startLifetime;
// error: Cannot implicitly convert type 'UnityEngine.ParticleSystem.MinMaxCurve' to 'float'

我认为答案与minMaxCurve constant变量有关,因为这会编译:

m_OriginalLifetime = m_System.main.startLifetime.constant;

但是文档中几乎没有解释。任何人都可以对此有所了解吗?

此外,新的乘数适合哪里?我假设你以前可以做到这一点:

particle.startSize *= myMultiplier

......你现在应该这样做吗?

particle.main.startSizeMultiplier = myMultiplier

1 个答案:

答案 0 :(得分:16)

<强> particle.startLifetime:

首先,Unity在Unity 5.5中所做的是为ParticleSystem添加新的未来。他们还暴露了之前隐藏的一些ParticleSystem API。

ParticleSystem.MainModule.startLifetime现在是MinMaxCurve的一种,而不是像ParticleSystem.startLifetime那样的浮动。

通过这样做,您现在可以获得更多选项,例如将startLifetime修改为曲线。

阅读或写入ParticleSystem.MainModule.startLifetime取决于通过编辑或代码设置的ParticleSystem.MainModule.startLifetime.mode的值。

enter image description here

ParticleSystem.MainModule.startLifetime.mode的默认值为ParticleSystemCurveMode.Constant

所以你的m_OriginalLifetime = m_System.main.startLifetime.constant;没问题。

如果startLifetime在运行时动态或随机更改为其他模式,则您必须执行以下操作:

ParticleSystem m_System = GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = m_System.main;

ParticleSystem.MinMaxCurve minMaxCurve = main.startLifetime;

if (minMaxCurve.mode == ParticleSystemCurveMode.Constant)
{
    m_OriginalLifetime = m_System.main.startLifetime.constant;
}
else if (minMaxCurve.mode == ParticleSystemCurveMode.Curve)
{
    AnimationCurve animCurveLifetime = m_System.main.startLifetime.curve;
}
...

<强> particle.startSize:

同样的事情适用于particle.startSizeparticle.startSize属性现在为m_System.main.startSize;

虽然您无法执行m_System.main.startSize.constant *= myMultiplier;,因为旧代码为particle.startSize *= myMultiplier

您需要获取m_System.main.startSize,然后对其进行修改,然后将修改后的m_System.main.startSize分配回m_System.main.startSize

particle.startSize *= myMultiplier应该是:

ParticleSystem m_System = GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = m_System.main;

ParticleSystem.MinMaxCurve minMaxCurve = main.startSize; //Get Size

minMaxCurve.constant *= myMultiplier; //Modify Size
main.startSize = minMaxCurve; //Assign the modified startSize back

然后,particle.main.startSizeMultiplierparticle.main.startSize用于什么?

这两个变量也可用于更改startLifetimestartSize。它的主要优点是效率很高。它并不要求您像我们上面那样复制MinMaxCurve,以便更改startSizestartSizeMultiplier

ParticleSystem m_System = GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = m_System.main;
main.startSizeMultiplier = 5;

ParticleSystem m_System = GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = m_System.main;
main.startLifetimeMultiplier = 8;

如果ParticleSystem.MainModule.startLifetime.mode不变,请使用它们。这将有效地改变整体寿命乘数或整体尺寸乘数。

更改颜色和颜色模式

<强>颜色

有一个隐式运算符,允许您使用:

ParticleSystem.MainModule main = trailPartical.main;
main.startColor = Color.red;

startColor实际上并不是Color的类型。 startColor变量现在是ParticleSystem.MinMaxGradient的一种类型。

这就是你应该如何改变粒子startColor

//Create Color
ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient();
color.mode = ParticleSystemGradientMode.Color;
color.color = Color.red;

//Assign the color to your particle
ParticleSystem.MainModule main = trailPartical.main;
main.startColor = color;

<强>渐变

public ParticleSystem particleSystem;

void Start()
{
    //Create Gradient key
    GradientColorKey[] gradientColorKey;
    gradientColorKey = new GradientColorKey[3];
    gradientColorKey[0].color = Color.red;
    gradientColorKey[0].time = 0f;
    gradientColorKey[1].color = Color.blue;
    gradientColorKey[1].time = 0.5f;
    gradientColorKey[2].color = Color.green;
    gradientColorKey[2].time = 1f;

    //Create Gradient alpha
    GradientAlphaKey[] gradientAlphaKey;
    gradientAlphaKey = new GradientAlphaKey[3];
    gradientAlphaKey[0].alpha = 1.0f;
    gradientAlphaKey[0].time = 0.0f;
    gradientAlphaKey[1].alpha = 0.5f;
    gradientAlphaKey[1].time = 0.5f;
    gradientAlphaKey[2].alpha = 1f;
    gradientAlphaKey[2].time = 1f;

    //Create Gradient
    Gradient gradient = new Gradient();
    gradient.SetKeys(gradientColorKey, gradientAlphaKey);

    //Create Color from Gradient
    ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient();
    color.mode = ParticleSystemGradientMode.Gradient;
    color.gradient = gradient;

    //Assign the color to particle
    ParticleSystem.MainModule main = particleSystem.main;
    main.startColor = color;
}

两种颜色之间的随机

//Create Color from Gradient
ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient();
color.mode = ParticleSystemGradientMode.TwoColors;
color.colorMin = Color.red;
color.colorMax = Color.green;

//Assign the color to the particle
ParticleSystem.MainModule main = particleSystem.main;
main.startColor = color;

两个渐变之间的随机

public ParticleSystem particleSystem;

void Start()
{

    //Create Gradient key Min
    GradientColorKey[] gradientColorKeyMin;
    gradientColorKeyMin = new GradientColorKey[3];
    gradientColorKeyMin[0].color = Color.red;
    gradientColorKeyMin[0].time = 0f;
    gradientColorKeyMin[1].color = Color.blue;
    gradientColorKeyMin[1].time = 0.5f;
    gradientColorKeyMin[2].color = Color.green;
    gradientColorKeyMin[2].time = 1f;

    //Create Gradient alpha Min
    GradientAlphaKey[] gradientAlphaKeyMin;
    gradientAlphaKeyMin = new GradientAlphaKey[3];
    gradientAlphaKeyMin[0].alpha = 1.0f;
    gradientAlphaKeyMin[0].time = 0.0f;
    gradientAlphaKeyMin[1].alpha = 0.5f;
    gradientAlphaKeyMin[1].time = 0.5f;
    gradientAlphaKeyMin[2].alpha = 1f;
    gradientAlphaKeyMin[2].time = 1f;

    //Create Gradient key Max
    GradientColorKey[] gradientColorKeyMax;
    gradientColorKeyMax = new GradientColorKey[3];
    gradientColorKeyMax[0].color = Color.red;
    gradientColorKeyMax[0].time = 0f;
    gradientColorKeyMax[1].color = Color.blue;
    gradientColorKeyMax[1].time = 0.5f;
    gradientColorKeyMax[2].color = Color.green;
    gradientColorKeyMax[2].time = 1f;

    //Create Gradient alpha Max
    GradientAlphaKey[] gradientAlphaKeyMax;
    gradientAlphaKeyMax = new GradientAlphaKey[3];
    gradientAlphaKeyMax[0].alpha = 1.0f;
    gradientAlphaKeyMax[0].time = 0.0f;
    gradientAlphaKeyMax[1].alpha = 0.5f;
    gradientAlphaKeyMax[1].time = 0.5f;
    gradientAlphaKeyMax[2].alpha = 1f;
    gradientAlphaKeyMax[2].time = 1f;

    //Create Gradient Min
    Gradient gradientMin = new Gradient();
    gradientMin.SetKeys(gradientColorKeyMin, gradientAlphaKeyMin);

    //Create Gradient Max
    Gradient gradientMax = new Gradient();
    gradientMax.SetKeys(gradientColorKeyMax, gradientAlphaKeyMax);


    //Create Color from Gradient
    ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient();
    color.mode = ParticleSystemGradientMode.TwoGradients;
    color.gradientMin = gradientMin;
    color.gradientMax = gradientMax;

    //Assign the color to the particle
    ParticleSystem.MainModule main = particleSystem.main;
    main.startColor = color;
}

随机颜色

public ParticleSystem particleSystem;

void Start()
{

    //Create Gradient key Min
    GradientColorKey[] gradientColorKeyMin;
    gradientColorKeyMin = new GradientColorKey[3];
    gradientColorKeyMin[0].color = Color.red;
    gradientColorKeyMin[0].time = 0f;
    gradientColorKeyMin[1].color = Color.blue;
    gradientColorKeyMin[1].time = 0.5f;
    gradientColorKeyMin[2].color = Color.green;
    gradientColorKeyMin[2].time = 1f;

    //Create Gradient alpha Min
    GradientAlphaKey[] gradientAlphaKeyMin;
    gradientAlphaKeyMin = new GradientAlphaKey[3];
    gradientAlphaKeyMin[0].alpha = 1.0f;
    gradientAlphaKeyMin[0].time = 0.0f;
    gradientAlphaKeyMin[1].alpha = 0.5f;
    gradientAlphaKeyMin[1].time = 0.5f;
    gradientAlphaKeyMin[2].alpha = 1f;
    gradientAlphaKeyMin[2].time = 1f;

    //Create Gradient key Max
    GradientColorKey[] gradientColorKeyMax;
    gradientColorKeyMax = new GradientColorKey[3];
    gradientColorKeyMax[0].color = Color.red;
    gradientColorKeyMax[0].time = 0f;
    gradientColorKeyMax[1].color = Color.blue;
    gradientColorKeyMax[1].time = 0.5f;
    gradientColorKeyMax[2].color = Color.green;
    gradientColorKeyMax[2].time = 1f;

    //Create Gradient alpha Max
    GradientAlphaKey[] gradientAlphaKeyMax;
    gradientAlphaKeyMax = new GradientAlphaKey[3];
    gradientAlphaKeyMax[0].alpha = 1.0f;
    gradientAlphaKeyMax[0].time = 0.0f;
    gradientAlphaKeyMax[1].alpha = 0.5f;
    gradientAlphaKeyMax[1].time = 0.5f;
    gradientAlphaKeyMax[2].alpha = 1f;
    gradientAlphaKeyMax[2].time = 1f;

    //Create Gradient Min
    Gradient gradientMin = new Gradient();
    gradientMin.SetKeys(gradientColorKeyMin, gradientAlphaKeyMin);

    //Create Gradient Max
    Gradient gradientMax = new Gradient();
    gradientMax.SetKeys(gradientColorKeyMax, gradientAlphaKeyMax);


    //Create Color from Gradient
    ParticleSystem.MinMaxGradient color = new ParticleSystem.MinMaxGradient();
    color.mode = ParticleSystemGradientMode.RandomColor;
    color.gradientMin = gradientMin;
    color.gradientMax = gradientMax;


    //Assign the color to the particle
    ParticleSystem.MainModule main = particleSystem.main;
    main.startColor = color;
}