- Home /
Question by
B-R-J · Feb 06, 2018 at 03:50 PM ·
colorcolor changec# tutorialgradient
convert gradient in to color keys
I am trying to change particle color over lifetime gradient by only using C# code. Is it possible to implement a gradient like this using code, without gradient editor? The documentation has one made for Blue->Red gradient, But it is difficult to figure out the below gradient with multiple colors.
untitled.png
(5.1 kB)
Comment
Best Answer
Answer by Hellium · Feb 06, 2018 at 04:03 PM
I don't know the exact values, but you will get the idea, nothing difficult. Just retrieve the exact colors and times using the inspector, and tweak them in the code if you want.
ParticleSystem ps = GetComponent<ParticleSystem>();
var col = ps.colorOverLifetime;
col.enabled = true;
Gradient gradiend = new Gradient();
GradientColorKey[] colorKeys = new GradientColorKey[3];
GradientAlphaKey[] alphaKeys new GradientAlphaKey[2];
alphaKeys[0].time = 0 ;
alphaKeys[0].alpha = 1;
alphaKeys[1].time = 1 ;
alphaKeys[1].alpha = 1;
colorKeys[0].time = 0.079;
colorKeys[0].color = Color.white;
colorKeys[1].time = 0.15;
colorKeys[1].color = new Color32(R, G, B);
colorKeys[2].time = 0.5;
colorKeys[2].color = new Color32(R, G, B);
gradiend.SetKeys(colorKeys, alphaKeys);
col.color = grad;