# Need some optimization tips (Newbie)

I’ve been working on a new project : attempting to make a 2D game like this :

Basically, I’ll be saving the values for each block in a huge 2D array, then drawing to the screen each block based on the camera’s position and the value of the blocks in the array.

However, simply drawing the blocks without even getting their value makes the game drop to 20 fps.

This is my code, it draws all the blocks to the screen and gives a moving impression when the camera moves :

``````using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Material mat;
int	ii = (int)(Mathf.Ceil(Screen.width/8)+1);
int	jj = (int)(Mathf.Ceil(Screen.height/8)+1);
int xx;
int yy;

void OnPostRender() {
GL.PushMatrix();
mat.SetPass(0);
GL.Color(new Color(0.5F, 0.5F, 0.5F, 1));

int i,j;

xx = (int)(transform.position.x%8);
yy = (int)(transform.position.y%8);

for(i=0; i<=ii; i+=1)
for(j=0; j<=jj; j+=1)
{
GL.Vertex3(i*8-xx, j*8-yy, 0);
GL.Vertex3(i*8-xx, j*8+7-yy, 0);
GL.Vertex3(i*8+7-xx, j*8+7-yy, 0);
GL.Vertex3(i*8+7-xx, j*8-yy, 0);
GL.End();
}
GL.PopMatrix();
}
}
``````

How would you suggest I do to draw all the blocks in the view to the screen, without any lag ? I’m really new to Unity so I don’t know which options I have. Could I for example draw the blocks once, copy the image to a huge sprite, then draw the sprite every step and update it only when a block changes, or would that be even worse ?

Thank you
Tabc3dd

I’d use the Mesh class to draw the level (probably using several meshes; it’s generally best if you keep vertices around 20K or so max), and do the darkness/lighting effects using vertex colors.

–Eric

Ok, thank you very much, I’ll see what i can do

Unfortunately, I don’t seem to have the knowledge to do so. Could you please give me a code that would create and render a grid mesh to the view please ? Also, would a grid mesh really be the best solution if i had to texture each blocks ?

Thank you
Tabc3dd