- Home /
Setting a variable to the amount of red pixels in a texture
Hi, i am trying to get the number of red pixels within a map texture and then store the amount of them in a variable to use for other things, What would be the best way to do this in c#
thanks
Answer by fafase · Oct 28, 2013 at 06:10 AM
You can use https://docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixels.html
And then check the value of each color. May be a little long though.
public Texture2D texture;
int GetRed()
{
int _result = 0;
Color[] _color = texture.GetPixels();
foreach(Color col in _color){
if(col == Color.red)_result++;
}
return _result;
}
This will return a value for any pixels fully red, you may want to makes it a little more flexible so that it includes color within a range of red like
if(col.r > 0.9 && col.g < 0.1 && col.b < 0.1)_result++;
this is regardless the alpha value.
If i then wanted to add each of the red pixels in to a list would that be possible?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Assigning current color to a variable for fade out (C#) 0 Answers
Variable is always false... 1 Answer