- Home /
Changing material Color using RBG?
Hello, Just to Be clear i am using JavaScript I am wondering if it is possible to change the color of a material using RBG values? I would like to make one GUI slider that accesses the numbers from 0-255 on R, G, B and alpha value. (That GUI slider would access all the possible colors from most right to most left.
I found this snippet of code to change the color of the material in RBG:
material.color = newColor(255f, 255f, 255f, 1);
Am I on the right track?
Thank You
Daniel
I would also like to be a will to change the shininess of the objects material
Answer by Eric5h5 · Jul 26, 2013 at 10:51 PM
If you want to use 0-255 integer values, use Color32.
material.color = Color32(255, 255, 255, 1);
This is good to know! Thanks for showing this. I'm sure it'll be useful.
Thank you! This is the answer I'm looking for, simple, straightforward!
For any one that wants to read more about Color32 http://docs.unity3d.com/Documentation/ScriptReference/Color32.html
Answer by LemonLake · Jul 26, 2013 at 10:36 PM
newColor, as far as I know, does not exist. Here's a function to create a color for what you want:
function getColor(red:int, green:int, blue:int, alpha:int):Color{
return Color((1/255)*red, (1/255)*green, (1/255)*blue, (1/255)*alpha);
}
(note: not tested, but should work :))
To use it, simply do:
material.color = getColor(255,255,255,255);
Red, green, blue and alpha are numbers between 0 and 255.
UPDATE: As for shininess, that is up to your shader. See http://docs.unity3d.com/Documentation/ScriptReference/Material.SetFloat.html
Looks like a typo, no space between the keyword new and Color. Just as a note, the keyword new and denotation f doesn't have to be included in uJS (though I like to use them!). uJS has a lot of implicit typecasting.
material.color = new Color( 0.1f, 0.1f, 0.1f, 1 );
// is the same as below in uJS
material.color = Color( 0.1, 0.1, 0.1, 1 );
Nice function =]
@alucardj Thanks for telling me, more into C# now, I last used javascript properly a few years ago. Fixed.
"f" doesn't have to be included in Unityscript because numbers with decimals are floats by default; if you want doubles then you need to specify "d". C# is the opposite, where numbers with decimals are doubles by default. You can specify "d" but it's not necessary, whereas you need to specify "f" if you want floats.
That function isn't necessary, since Color32 exists. Also it wouldn't work since you're doing integer division.
Excellent function however I'm leaning toward color32, because i can replace the numbers easily with floats and don't have to have integer division! BUT thank you for the shininess! How do i accept more than one answer it wont let me? I have seen it done before though!
Answer by AlucardJay · Jul 26, 2013 at 10:29 PM
http://docs.unity3d.com/Documentation/ScriptReference/Color.html
Each color component is a floating point value with a range from 0 to 1.
Try :
var r : float = value / 255.0f;
Shininess would be relative to the Shader.
But looks like color 32 is already 255? SO this would be only necessary if i was using the function lemon lake submitted correct?