首页 > 解决方案 > Unity如何将相同颜色的所有像素更改为另一种颜色?

问题描述

例如,我有一个 png 文件,我想将 png 文件中具有相同 RGB 值的所有像素转换为另一种颜色。我正在制作一个包含省份的交互式地图,我想要一个文件,其中所有省份都有不同的 RGB 值,并使用省份颜色作为 id 系统。问题是我不知道如何将 RGB 值更改为另一个

标签: unity3d

解决方案


如果它是 Texture2D,您可以按如下方式更改像素:

Texture2D texture = new Texture2D(128, 128); // load your texture here
Color colortrigger = Color.Blue; // color triggers to change
for (int y = 0; y < texture.height; y++)
{
    for (int x = 0; x < texture.width; x++)
    {
        if(texture.GetPixel(x,y) == colortrigger)
        {
           // Change the pixel to another color
           texture.SetPixel(x, y, Color.Yellow);              
        }
    }
}
texture.Apply();

推荐阅读