- Home /
Compare getPixel() to a Color
I am needing to use the TextureMap.GetPixel() in a switch and case statement. There are several specific colors that the ray will definitely hit which is why I am trying to use a switch case statement. The several colors that will be hit are Color(0.925,0.942,1.000,1.000) and Color(0.000,0.941,1.000,1.000) for example. I have tryed
var color = Color(0.925,0.942,1.000,1.000)
switch(TextureMap.GetPixel (pixelUV.x,pixelUV.y)){
case color:
print("This is southland");
break;
}
//also doesn't work
if(TextureMap.GetPixel (pixelUV.x,pixelUV.y) == color){
print("This is southland");
}
But this doesn't work even if when both color and getPixel() are printed and they are exactly the same. Also doesn't work in if statements. Can anyone see anything that is wrong with the code?
Answer by numberkruncher · May 28, 2013 at 08:38 AM
Whilst not necessary, I would recommend defining your selection colours as constants for cleaner code, but essentially the following would work:
// C#
public static readonly Color FirstColour = new Color(0.925f, 0.942f, 1f, 1f);
public static readonly Color SecondColour = new Color(0f, 0.941f, 1f, 1f);
public void Foo(Texture2D tex) {
Color pixel = tex.GetPixel(10, 10);
if (pixel == FirstColour) {
// ...
}
else if (pixel == SecondColour) {
// ...
}
}
// UnityScript
public static var FirstColour:Color = new Color(0.925f, 0.942f, 1f, 1f);
public static var SecondColour:Color = new Color(0f, 0.941f, 1f, 1f);
public function Foo(tex:Texture2D) {
var pixel:Color = tex.GetPixel(10, 10);
if (pixel == FirstColour) {
// ...
}
else if (pixel == SecondColour) {
// ...
}
}
Your answer
Follow this Question
Related Questions
create readable colour map 1 Answer
Material doesn't have a color property '_Color' 4 Answers
Using GetPixel to instantiate blocks of different pixel colors 1 Answer
Changing two different objects renderer colour 1 Answer
How to pixelate an image texture for faster pixel color recognition? 2 Answers