I’m trying to use the Gradient.SetKeys function to make a simple gradient from blue to yellow. I’m a bit confused with the syntax though.
I’ve got something like this:
var noteGradient = new Gradient();
var blueCol = new GradientColorKey(Color(0,0,0.7),0);
var yellowCol = new GradientColorKey(Color(1,1,0),1);
noteGradient.SetKeys(blueCol,1);
But it’s looking for a list of color keys? I’m using unityscript/javascript.
var noteGradient = new Gradient();
var blueCol = new GradientColorKey(Color(0,0,0.7),0);
var yellowCol = new GradientColorKey(Color(1,1,0),1);
var blueAlpha = new GradientAlphaKey(1,0);
var yellowAlpha = new GradientAlphaKey(1,1);
noteGradient.SetKeys([blueCol, yellowCol], [blueAlpha,yellowAlpha]);
With some experimenting I got it even more concise since I only need one alpha. noteGradient.SetKeys([blueCol, yellowCol], GradientAlphaKey(1,0)]);
And 1 object less is created, hurray for memory optimization! (Doesn't there need to be a new keyword before GradientAlphaKey? Again, I don't code in unityscript so I have no clue if that's mandatory.)
Thanks Unitraxx, that got me most of the way there. I actually had to do this:
var noteGradient = new Gradient();
var blueCol = new GradientColorKey(Color(0,0,0.7),0);
var yellowCol = new GradientColorKey(Color(1,1,0),1);
var blueAlpha = new GradientAlphaKey(1,0);
var yellowAlpha = new GradientAlphaKey(1,1);
var colorKeys : GradientColorKey[] = [blueCol, yellowCol];
var alphaKeys : GradientAlphaKey[] = [blueAlpha,yellowAlpha];
noteGradient.SetKeys(colorKeys, alphaKeys);
It seems cumbersome, but I guess that’s what it takes.
I use this construction, with using System.Collections.Generic.List maybe it will be useful to someone List<GradientColorKey> colorKeys = new List<GradientColorKey> { new GradientColorKey(new Color(0, 0, 0.7f), 0), new GradientColorKey(new Color(1, 1, 0), 1) }; List<GradientAlphaKey> alphaKeys = new List<GradientAlphaKey> { new GradientAlphaKey(0.3f, 0), new GradientAlphaKey(0.3f, 1) }; var gradient = new Gradient(); gradient.SetKeys(colorKeys.ToArray(), alphaKeys.ToArray());
With some experimenting I got it even more concise since I only need one alpha. noteGradient.SetKeys([blueCol, yellowCol], GradientAlphaKey(1,0)]);
– stwertAnd 1 object less is created, hurray for memory optimization! (Doesn't there need to be a
– Unitraxxnewkeyword beforeGradientAlphaKey? Again, I don't code in unityscript so I have no clue if that's mandatory.)It seems to work with or without "new"... I'm not very experienced in unityscript either, so I don't what is best.
– stwert