- Home /
How do I fill a texture with one color?
How do I fill a texture with one color, like red for example. I tried this;
var fillColor : Color = Color(1, 0.0, 0.0);
tex2.SetPixels( fillColor, 0 );
tex2.Apply();
But that does obviously not work, so how would I go about doing this?
Also posted here: http://forum.unity3d.com/threads/131205-How-do-I-fill-a-texture-with-one-color?p=885701#post885701
Comment
Best Answer
Answer by jukke · Apr 07, 2012 at 06:50 PM
Alright, this seem to work:
var fillColor : Color = Color(1, 0.0, 0.0);
var fillColorArray = tex2.GetPixels();
for(var i = 0; i < fillColorArray.Length; ++i)
{
fillColorArray[i] = fillColor;
}
tex2.SetPixels( fillColorArray );
tex2.Apply();
Just wanted to add that you don't need to call GetPixels.
var fillColorArray = new Color[tex2.width * tex2.height];
Answer by shraddhashashankpatil · Oct 13, 2015 at 11:58 AM
Texture2D targetTexture= new Texture2D(originalTexture.width, originalTexture.height);
GetComponent<Renderer>().material.mainTexture = targetTexture;
for (int y = 0; y < originalTexture.height; y++) {
for (int x = 0; x < originalTexture.width; x++) {
targetTexture.SetPixel(x, y, Color.green);
}
}
targetTexture.Apply();
Just a note that SetPixel() is much slower than SetPixels():
"Using SetPixels can be faster than calling SetPixel repeatedly, especially for large textures. In addition, SetPixels can access individual mipmap levels."