Fastest Way to Draw a Square

I am trying to draw many squares on the screen at once.

Currently I am using:

private static void GUIDrawRect(Rectposition,Colorcolor)
{
    _staticRectTexture.SetPixel(0,0,color);
    _staticRectTexture.Apply();

    _staticRectStyle.normal.background=_staticRectTexture;

    GUI.Box(position,GUIContent.none,_staticRectStyle);

}

I figured it would be faster to draw squres than creating hundreds of game objects
and rendering them.

I am currently calling this function from the OnGUI loop. I have been trying to look into GL calls, but I can’t seem to understand how to utilize them.

Any ideas is much appreciated. Thankyou

Use code tags:

I noticed you went through the effort of even formatting that code, when really, there’s built in formatting functionality specifically for code.

And no, it’s not going to be faster from the mono side to draw anything as it’s gotta call back through to the actual engine. It’ll be slow.

You want squares drawn, create a square, and allow unity to draw them for you. Batch them if they’re all identical.

ok I used code tags. sorry. Ok it is good to know that it is better to let unity do it’s own thing for optical rendering.

When you say “Create A Square” am I just creating a Sprite and then creating a prefab from that sprite?

If you want it as a Sprite, yes.

Also, if you plan to draw textures in code ever… modifying a texture (changing its colour) every call to render the texture is probably not a good idea. That’s expensive.

1 Like

Yep, the reason why I said sprite is because I am literally just drawing small white squares on the screen and there is no dynamic 3D camera or anything.

I also need a way to adjust the alpha of the Sqaure from 0 - Max Alpha over a certain period of time. Would a shader be the only alternative?