- Home /
Draw a pixel and change its color in code
I'm trying to draw a single GUI pixel, and be able to change its color. I tried this but I cannot figure out how to change the color. brush is a public Texture.
GUI.DrawTexture(new Rect(100,100,1,1),brush,ScaleMode.StretchToFill,false,0);
I tried changing brush to a Texture2D, then using brush.SetPixel(0,0,Color.red); but the compiler says 'texture is not readable, the texture memory cannot be accessed from scripts.
There's probably a really simple way to draw a single pixel and set the color. Let me know what that is please.
Answer by iwaldrop · Apr 17, 2013 at 02:37 AM
You could create your own texture and change it's values, or you could set the texture to readable.
To do the former, just make a new texture:
private Texture2D brush;
private int size = 256;
void Awake()
{
brush = new Texture2D(size, size);
ChangeTheColor(Color.red);
}
void ChangeTheColor(Color c)
{
for (int x = 0; x < size / 2; x++)
for (int y = 0; y < size / 2; y++)
brush.SetPixel(x, y, c);
brush.Apply();
}
void OnGUI()
{
GUILayout.Label(brush);
}
For the latter, browse to your texture import settings, set 'Texture Type' to 'Advanced', and check 'Read/Write Enabled'.
Thank you! I tried both concepts. Your first suggestion is the best for me, because the brush takes the exact color you specify, and this application only needs solid colors.
When I tried your latter solution, making the texture asset readable/writable, that worked also BUT, the color you Apply() overlays the color of the original asset. So you don't get the pure color. There is some mixing going on.
So solids, approach 1, fabrics and complex textures, approach 2.
Happiness.
I wish Apply() had some sort of blending mode like this, but it doesn't. Something else must have been causing it, outside of what has been described here.
Great to hear that you've gotten it to work! Awesome!
Could you do the board a favor though and accept the answer so this question will be marked as answered? Thanks, and happy coding! :)
And please don't post comments as answers - on UA Answer means Solution and not Reply.
Your answer
Follow this Question
Related Questions
GUI.DrawTexture not working 2 Answers
How to change rect size? 2 Answers
[Closed]GUI.DrawTexture inside GUI.DrawTexture 1 Answer
Why doesn't my texture show up? 1 Answer
create readable colour map 1 Answer