How to render a colored 2D rectangle.
I am new to Unity and trying to get a handle on some of the basic rendering functions, most of my experience is with C++ and OpenGL.
(I can create the texture elsewhere, I just included it here for clarity).
This renders a fullsized box with default GUI style, and the texture is shown at literal size as a 2x2 small red square where the text would normally appear.
I tried drawing the rectangle at the exact dimensions of the texture (2x2) and it shows up black, I assume that GUI.Box is not meant for general purpose rendering of colored rectangles.
This doesn't show up at all (I believe I am missing something).
I couldn't find an example of this in the docs or the forums, I'm assuming this is such an easy operation that it doesn't need to be described in detail anywhere but I am stuck at this point, I would appreciate any push in the right direction.
(The display seems to mangle the code in the originalpost but this looks ok)..
This works but is major overkill for a generic rectangle-drawing routine, can anyone clarify the correct way of drawing a basic colored rectangle?
I can use this for now but it's definitely a bad hack, I believe I'm missing something and I haven't found a clear solution in the docs, thanks again for anyone that can point me in the right direction..
Is there some reason you need to use a texture? I don't use Unity GUI; I just draw meshes based on coordinates of an easy-to-code for orthographic camera, and use a solid color shader on them.
I'm only using the texture because that's what GUI.Box needed for specifying a color, I'm open to whatever the correct solution is.
I considered using temporary 3d meshes with an orthographic projection but that seems like the long way to go around just to draw a colored 2d rectangle, I assumed there would be some basic 2d drawing routines included somewhere.
If you can verify that's the best solution for Unity and it's not really feasible any other way I can do it, I'm ok with work-arounds if that's how it has to be..
Unity isn't geared for 2D. Unity GUI isn't geared for drawing anything that isn't textured. I don't see a good match for what you need. Personally, I do my 2D work with scripted quads, orthographic cameras, and simple custom shaders. It's more complicated than I'd like, but it's not really any messier than what you have above, and it's way better on performance, especially if you can combine your rectangles into one mesh, or at least share a material, for batching. Unity GUI items all require a draw call.
Thanks Jessy, this is the verification I was looking for, I expected that Unity might be really "built" for 3d and not much else, now I know the correct direction I need to go in..
Don't use this code as is. if the above function is called from onGUI, it creates a memory leak because a new texture is being created every frame. Secondly, SetPixel is very very slow and should be avoided on mobile.
I know this question is about GUI. So far I've found the best performance for transparent shades is to use super lightweight shaders on meshes for large transparent elements.
It was useful for me, thank you!
what about memory leak with this code? Is this safe to use?
It is NOT safe, I have experienced running out of memory on a Windows platform with exactly this code, because the Texture2D doesn't get deallocated automatically (although it really should). If you need this code, the fix is to only call 'new Texture2D(1,1)' once and store it in a static variable in your class. On OS X platforms I didn't experience the problem.
Here's one way to do it with GL.Begin/GL.End. You should also be able to draw a mesh similarly with Graphics.DrawMeshNow. Please select a material that accepts color. I just tested this quickly with GUI/Text Shader but you will probably want to use a solid color shader that uses the vertex color channel.
// Please assign a material that is using position and color.
// We shouldn't draw until we are told to do so.
This will render a red rectangle.
I found a way to draw a box inside OnGUI that only uses a very small texture, and it works on my machine (mac laptop).
I use this to render a 50% transparent white box on the entire screen, including the GUI elements. Just substitute the color values for whatever color you need. You could even make gradients by setting different colors in this 2 by 2 pixel texture.