c# - Unity 5.5 废弃的粒子系统代码

标签 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

最佳答案

particle.startLifetime:

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

ParticleSystem.MainModule.startLifetime 现在是 MinMaxCurve 的一种而不是像 ParticleSystem.startLifetime 那样 float .

通过这样做,您现在可以获得更多选项,例如修改 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.startSize . particle.startSize属性(property)现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;
}

关于c# - Unity 5.5 废弃的粒子系统代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393814/

相关文章:

c# - TFS WorkItemChangedEvent 触发两次

c# - Smartsheet c# SDK 父子

unity3d - 来自 shadow caster 2D 的阴影长度是无限的

git - 设置 Unity 智能 merge

android - AGP版本升级失败

ios - 更改为独立应用程序后的 Xcode 更新

c# - 从父类型属性的子集创建匿名类型

c# - 如何从 View 中的 FOREACH 语句输出 MVC 中的字符串列表?

unity3d - #pragma kernal Main上的语法错误

php - 无法将 Laravel 5.4 更新到 5.5 Composer