如何获得精灵的平均颜色?(Unity3d)

use*_*071 3 unity-game-engine

我正在完成我要放在资产商店中的资产。现在,其中一项功能需要精灵的平均颜色。现在我有一个公共颜色来查找平均颜色,用户可以使用颜色选择器或色轮或其他任何东西来选择他们认为看起来像精灵的平均颜色。我想让脚本自动计算平均精灵颜色,从而通过消除人为错误来提高准确性,并通过不浪费用户时间猜测平均精灵颜色来提高效率。

小智 5

Unity 论坛中有一篇关于它的帖子。这里是链接。答案是:

Color32 AverageColorFromTexture(Texture2D tex)
{

        Color32[] texColors = tex.GetPixels32();

        int total = texColors.Length;

        float r = 0;
        float g = 0;
        float b = 0;

        for(int i = 0; i < total; i++)
        {

            r += texColors[i].r;

            g += texColors[i].g;

            b += texColors[i].b;

        }

        return new Color32((byte)(r / total) , (byte)(g / total) , (byte)(b / total) , 0);

}
Run Code Online (Sandbox Code Playgroud)