Unity 5.5过时的粒子系统代码
之前5.5粒子系统变量可以通过ParticleSystem访问并进行读/写。 现在它们是通过ParticleSystem.MainModule访问的,因此很多代码已经过时了。 API更新程序无法解决大多数问题。 我已经阅读了新文档,但我无法弄清楚应该如何使用新的变量类型。 例如,在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 常量变量有关,因为这会编译:
m_OriginalLifetime = m_System.main.startLifetime.constant;
但是文档中几乎没有解释。 任何人都可以对此有所了解吗?
此外,新的乘数适合哪里? 我假设你以前可以做到这一点:
particle.startSize *= myMultiplier
……你现在应该这样做吗?
particle.main.startSizeMultiplier = myMultiplier
particle.startLifetime:
首先,Unity在Unity 5.5中所做的是为ParticleSystem
添加新的未来。 他们还暴露了之前隐藏的一些ParticleSystem
API。
ParticleSystem.MainModule.startLifetime
现在是一种MinMaxCurve
而不是像ParticleSystem.startLifetime
那样的浮点数。
通过这样做,您现在startLifetime
更多选项,例如将startLifetime
修改为曲线。
读取或写入ParticleSystem.MainModule.startLifetime
取决于通过编辑器或代码设置的ParticleSystem.MainModule.startLifetime.mode
的值。
ParticleSystem.MainModule.startLifetime.mode
的默认值是ParticleSystemCurveMode.Constant
所以你的m_OriginalLifetime = m_System.main.startLifetime.constant;
很好。
如果在运行时动态或随机地将startLifetime
更改为其他模式 ,则必须执行以下操作:
ParticleSystem m_System = GetComponent(); 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
属性现在是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.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.startSizeMultiplier
和particle.main.startSize
用于什么?
这两个变量也可用于更改startLifetime
和startSize
。 它的主要优点是效率很高。 它不需要像上面那样制作MinMaxCurve
的副本,以便更改startSize
或startSizeMultiplier
。
ParticleSystem m_System = GetComponent(); ParticleSystem.MainModule main = m_System.main; main.startSizeMultiplier = 5;
和
ParticleSystem m_System = GetComponent(); 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; }