c#结合unity找出图片像素中出现最多的颜色
public class Test : MonoBehaviour
{
Dictionary<Color, int> cache = new Dictionary<Color, int>();
public Texture2D img;
void Start()
{
Comparer();
GetMax();
}
void Comparer()
{
for (int i = 0; i <= img.width; i++)
{
for (int j = 0; j <= img.height; j++)
{
Color jj = img.GetPixel(i, j);
if (cache.ContainsKey(jj))
{
cache[jj] += 1;
}
else
{
cache[jj] = 1;
}
}
}
}
void GetMax()
{
Color c1 = Color.red;
int t = -1;
foreach (var hu in cache)
{
if (hu.Value >= t)
{
t = hu.Value;
c1 = hu.Key;
}
}
Debug.LogErrorFormat("{0},{1}", c1, t);
}
}
网友评论