- Home /
How can I draw one black pixel in GUI
What is the most optimized way of drawing one black pixel in my GUI and how do I achieve that?
This is my attempt, but it does not show anything (requires unity pro):
GL.Begin(GL.LINES);
GL.Color(new Color(0, 0, 0));
GL.Vertex3(drawPosition.xMin, drawPosition.yMin, 0);
GL.End();
GL.PopMatrix();
Answer by Mortennobel · Apr 18, 2011 at 10:00 AM
I would think it is much more efficient to draw on a texture, and then draw the texture on the screen. But it depends on how many pixels you are painting and how often you update your pixels.
Take a look at these two methods:
http://unity3d.com/support/documentation/ScriptReference/Texture2D.SetPixel.html
http://unity3d.com/support/documentation/ScriptReference/GUI.DrawTexture.html
Anyway the reason your code does not work is that you are trying to draw a line of only one point (which means a line of length 0 which equals nothing). You could try to use GL.Begin(0); instead (meaning GL_POINTS - but there is no unity wrapper for this .. most likely for a reason, so it may not work)
Answer by CHPedersen · Apr 18, 2011 at 10:39 AM
Texture2D.SetPixel does the trick indeed, but make sure you don't do this repeatedly in real-time. SetPixel is in itself not particularly fast, and to make matters worse it must be followed by a call to Texture2D.Apply, which uploads the texture to the graphics card so it can be displayed. Uploading textures is exceptionally costly because it's limited by bus bandwidth, so don't do that unless absolutely necessary.
You seem to have unity pro, which gives you render textures. Consider using them and a camera to render a dot onto a texture, then display that instead. This ensures the texture is created at the graphics card's side, which is must faster.