我假设您的意思是更改单个像素?在这种情况下,请使用 Texture2D 类的 GetData() 和 SetData() 方法。
例如,您可以通过执行以下操作获取包含单个像素颜色的数组:
// Assume you have a Texture2D called texture
Color[] data = new Color[texture.Width * texture.Height];
texutre.GetData(data);
// You now have a packed array of Colors.
// So, change the 3rd pixel from the right which is the 4th pixel from the top do:
data[4*texture.Width+3] = Color.Red;
// Once you have finished changing data, set it back to the texture:
texture.SetData(data);
请注意,您可以使用 GetData() 的其他重载来仅选择一个部分。
因此,将指定颜色的每个像素替换为另一种颜色:
// Assume you have a Texture2D called texture, Colors called colorFrom, colorTo
Color[] data = new Color[texture.Width * texture.Height];
texutre.GetData(data);
for(int i = 0; i < data.Length; i++)
if(data[i] == colorFrom)
data[i] = colorTo;
texture.SetData(data);
要查看色调是否相似,请尝试以下方法:
private bool IsSimilar(Color original, Color test, int redDelta, int blueDelta, int greenDelta)
{
return Math.Abs(original.R - test.R) < redDelta && Math.Abs(original.G - test.G) < greenDelta && Math.Abs(original.B - test.B) < blueDelta;
}
其中 *delta 是您想要接受的每个颜色通道的变化容差。
要回答您的编辑,没有内置函数,但您可以混合使用上述两个部分的想法:
Color[] data = new Color[texture.Width * texture.Height];
texutre.GetData(data);
for(int i = 0; i < data.Length; i++)
if(IsSimilar(data[i], colorFrom, range, range, range))
data[i] = colorTo;
texture.SetData(data);