Hi-
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.
To start with I would like to render simple colored rectangles in 2d screen space, I've gotten as far as the following C# code (in OnGUI()):
Texture2D red_texture = new Texture2D(2, 2); Color red_color = new Color(1, 0, 0); red_texture.SetPixel(0, 0, red_color); red_texture.SetPixel(1, 0, red_color); red_texture.SetPixel(1, 1, red_color); red_texture.SetPixel(0, 1, red_color); red_texture.Apply(); red_texture.wrapMode = TextureWrapMode.Repeat; GUI.Box (new Rect (0,0,100,100), red_texture);
(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.
I also tried the following code in OnDrawGizmos():
Texture2D green_texture = new Texture2D(2, 2); Color green_color = new Color(0, 1, 0); green_texture.SetPixel(0, 0, green_color); green_texture.SetPixel(1, 0, green_color); green_texture.SetPixel(1, 1, green_color); green_texture.SetPixel(0, 1, green_color); green_texture.Apply(); green_texture.wrapMode = TextureWrapMode.Repeat; Gizmos.DrawGUITexture(new Rect(100, 100, 200, 200), green_texture);
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.
Thanks in advance for any help -MM
UPDATE:
I managed to get a GUI-box with no border by using a default GUIStyle for the box rendering, but the downside is the texture image has to be the exact size of the desired rectangle, here is the code:
void Render_Colored_Rectangle(int x, int y, int w, int h, float r, float g, float b)
{
Texture2D rgb_texture = new Texture2D(w, h);
Color rgb_color = new Color(r, g, b);
int i, j;
for(i = 0;i<w;i++)
{
for(j = 0;j<h;j++)
{
rgb_texture.SetPixel(i, j, rgb_color);
}
}
rgb_texture.Apply();
GUIStyle generic_style = new GUIStyle();
GUI.skin.box = generic_style;
GUI.Box (new Rect (x,y,w,h), rgb_texture);
}
(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..