- Home /
Using predefined colors vs defining new colors
I have a script that recolors a number of objects at runtime, using someObject.renderer.material.color = color. I'm pulling these colors from a predefined list. For some reason, if I fill that list with the predefined colors in unity, everything works fine. If I try to define my own colors, they show up as unlit colors.
Here's my code, and an example of the difference between the two:
List<Color> colorList = new List<Color>(){
Color.red,
Color.green,
Color.yellow,
Color.magenta,
new Color(255F, 0F, 255F),
new Color(0F, 255F, 255F),
new Color(255F, 255F, 0F),
new Color(128F, 0F, 128F),
new Color(128F, 0F, 0F)
};
someModel.renderer.material.color = colorList[i];
Answer by rutter · Oct 22, 2013 at 10:43 PM
Unity's Color expects values between 0 and 1. You can swap 255 for 1.0, 128 for 0.5.
Yeah, that's confusing for C# folk who are used to the standard .NET color constructor that uses 0-255 for RGBA values.
Just for completeness, you can also use the Color32 struct to define your colors. The Color32 constructor takes 4 byte values between 0 and 255. There's only one constructor so you have to pass the alpha value as well.
As you can see at the bottom of the scripting reference it has two implicit casting operators which allows you to assign a Color32 value directly to a Color value or vice versa.
So you can simply do this:
List<Color> colorList = new List<Color>(){
Color.red,
Color.green,
Color.yellow,
Color.magenta,
new Color32(255, 0, 255, 255),
new Color32( 0, 255, 255, 255),
new Color32(255, 255, 0, 255),
new Color32(128, 0, 128, 255),
new Color32(128, 0, 0, 255)
};
0-255 isn't a dot-NET thing. RGBA32 has been stored as 1-byte per channel = 0-255 for a long time.
But shaders have "always" used 0-1 for colors, and that range is also a little nicer for blending.
If you like, write: Color makeCol(int r...) { return new Color(r/255.0f...)}
Owen - I think you missed my point - I'm not suggesting that 0-255 is a dot-NET thing ;) I'm suggesting that most C# programmers co$$anonymous$$g to Unity will see a Color() structure and assume it's the one from the System.Drawing namespace (which represents RGBA components as a value from 0-255), not Unity's own Color() structure from the UnityEngine namespace (which represents compononent values in the range 0.0 - 1.0). And both of which are unrelated to the representation of a colour in Cg/GLSL as used in a shader.
Thank you all for your help and explanations - it makes a bit more sense why Color wants float values. I've switched over to Color32, and everything is now working exactly as desired. Thanks again!
Your answer
Follow this Question
Related Questions
Vertex colors as diffuse in lightmapping. 0 Answers
My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers
how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers
Color of some instantiated objects differs from the normal ones. 1 Answer