The goal is to remove the particles via script with fade effect. But I'm not just interested in an alternative, I want to understand why my code doesn't work.
The code below is modified to show only the important parts. I can't know before what time the particles will be dissolved, so I can't set the parameter from the editor.
StarsConfig.Stars.colorOverLifetime.color.gradient.SetKeys
This is the important point (see the log image). The gradient does not take my new values but alphaKey
is correct.
Why ? Thanks
private bool FadeStarts = false;
void Update()
{
if (FadeStarts) return;
StartCoroutine(fadeStars(false));
}
private IEnumerator fadeStars(bool show)
{
FadeStarts = true;
ParticleSystem.ColorOverLifetimeModule c = new ParticleSystem.ColorOverLifetimeModule();
c = StarsConfig.Stars.colorOverLifetime;
Gradient g = c.color.gradient;
Debug.Log("OFF " + c.color.gradient.alphaKeys[0].alpha + " --> " + g.alphaKeys[0].alpha);
GradientAlphaKey[] alphaKey = new GradientAlphaKey[2];
alphaKey[0] = c.color.gradient.alphaKeys[0];
alphaKey[1] = c.color.gradient.alphaKeys[1];
while (c.color.gradient.alphaKeys[0].alpha > 0f) {
alphaKey[0].alpha -= StarsConfig.VelocityAlpha * Time.deltaTime;
alphaKey[1].alpha -= StarsConfig.VelocityAlpha * Time.deltaTime;
StarsConfig.Stars.colorOverLifetime.color.gradient.SetKeys(c.color.gradient.colorKeys, alphaKey);
Debug.Log("g " + alphaKey[0].alpha + " --> " + StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[0].alpha);
/*
StarsConfig.Stars.colorOverLifetime.color.gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(Color.white, 0f),
new GradientColorKey(Color.white, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[0].alpha - (StarsConfig.VelocityAlpha * Time.deltaTime), 0f),
new GradientAlphaKey(StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[1].alpha - (StarsConfig.VelocityAlpha * Time.deltaTime), 1f) });
*/
yield return 0;
}
StarsConfig.Stars.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
FadeStarts = false;
}
UPDATE
Ed_Marty's suggestion is correct, and the solution is there (thanks!). But I would like to understand what is the difference between these two codes.
This code works
var col = StarsConfig.Stars.colorOverLifetime;
while (col.color.gradient.alphaKeys[0].alpha > 0f) {
float alpha = StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[0].alpha - (StarsConfig.VelocityAlpha * Time.deltaTime);
Gradient grad = new Gradient();
grad.SetKeys(
new GradientColorKey[] {
new GradientColorKey(Color.white, 0.0f),
new GradientColorKey(Color.white, 1.0f) },
new GradientAlphaKey[] {
new GradientAlphaKey(alpha, 0.0f),
new GradientAlphaKey(alpha, 1.0f) });
col.color = grad;
yield return 0;
}
And this doesn't work
while (StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[0].alpha > 0f) {
float alpha = StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[0].alpha - (StarsConfig.VelocityAlpha * Time.deltaTime);
StarsConfig.Stars.colorOverLifetime.color.gradient.SetKeys(
new GradientColorKey[] { new GradientColorKey(Color.white, 0f),
new GradientColorKey(Color.white, 1.0f) },
new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0f),
new GradientAlphaKey(alpha, 1f) });
yield return 0;
}
Gradient g
but I don't use it, because it has remained from the previous tests. \$\endgroup\$