我想要做的是循环检查特定颜色的像素并计算它们的图片。这对我的项目至关重要。
所以问题是,即使颜色相同,将 GetPixel 与 Color 进行比较也永远不会返回 true。
我使用单色方形纹理对其进行了测试,并调试记录了 GetPixel 的值,它是:“RGB(0.098,0.451,0.000,1.00)”,这是我正在寻找的颜色,但它仍然返回错误。
我也在 Unity (UnityScript) 中使用 JavaScript,我没有错误。RGB 值是百分比,是的,我启用了纹理读取。
这是代码,请帮助我。
#pragma strict
public static function PixelMagic()
{
var slika:Texture2D = Resources.Load("backgr",Texture2D);
var pixel_color:Color = new Color(0f,0f,0f,0f);
var green:Color = new Color(0.098f,0.451f,0f,1f);
var hit = 0;
for(var x=0; x<slika.width; x++)
{
for(var y=0; y<slika.height; y++)
{
pixel_color = slika.GetPixel(x,y);
if(green==pixel_color)
{
hit++;
}
}
}
Debug.Log(hit);
Debug.Log(pixel_color);
}