How to manage a grid game like Xonix

Hi guys, I’m a newbie in Unity as well as game making. I’m trying to create a 2D Xonix-like game that needs to store a lot of grid objects. A grid objects contains of {Transform} and {SpriteRenderer}.

The problem is, when I create a 1515 grid, the player is still fine. But when I put the grid onto 3030, it is very laggy.

Here is the code that I’m using:

  1. Generating Grid from a file contains character ‘x’ and ‘o’:

     	int xx = 0;
     	int yy = gridHeight;
     	foreach (char c in text) 
     	{
     		if (c == '\r') continue;
     		if (c == '
    

') {
xx = 0;
yy–;
}
else {
xx++;
gridScript.grid[xx, yy] =
Instantiate(normalTile, new Vector3((float)xx, (float)yy, 8f), Quaternion.identity) as GameObject;
if (c == ‘x’) {
gridScript.GridDisable(gridScript.grid[xx, yy]);
gridScript.totalGrid–;
}
else
{
gridScript.GridEnable(gridScript.grid[xx, yy]);
}
}
}

		gridScript.remainingGrid = gridScript.totalGrid;
	}
}
  1. The GridEnable and GridDisable Script:

    public void GridEnable(GameObject tile)
    {
    tile.renderer.enabled = true;
    int x = Mathf.RoundToInt (tile.transform.localPosition.x);
    int y = Mathf.RoundToInt (tile.transform.localPosition.y);
    gridStatus [x, y] = 0;
    }

     public void GridDisable(GameObject tile)
     {
     	tile.renderer.enabled = false;
     	int x = Mathf.RoundToInt (tile.transform.localPosition.x);
     	int y = Mathf.RoundToInt (tile.transform.localPosition.y);
     	gridStatus [x, y] = 1;
     }
    

Sorry for my bad English (it’s not my native language).

1 Answer

1

Hey i’m making a randomly generated grid based game right now, mines 3d and doesn’t cause any lag at 50x50. Dunno if this script can help you but heres how i’m doing it.

public Transform grassLand;
public Transform dirtRoad;
public Transform city;
public Transform wolf;
public Transform bandit;

public int randNum = 0;
public int xCubes;
public int zCubes;

public bool fighting = false;
public bool enterWolf = false;

void Start () {
   for ( int z = 0; z < zCubes; z++ )
     {
      for ( int x = 0; x < xCubes; x++ )
      {
          float rnd = Random.value;
          if ( rnd < 0.05 )
          {
             Instantiate(city, new Vector3(x, 0, z), Quaternion.identity);
          }
          else if ( rnd < 0.15 )
          {
          Instantiate(dirtRoad, new Vector3(x, 0, z), Quaternion.identity);
          }
          else 
          {
          Instantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity);
          randNum = Random.Range(0,50);
          if(randNum <= 3){
             Instantiate(wolf,new Vector3(x,0,z), Quaternion.identity);
             }
          else if(randNum ==4){
             Instantiate(bandit, new Vector3(x,0,z), Quaternion.identity);
          }
          }
      }
     }

`

All you’d have to do to make it 2d is change the vector3’s to Vector3(x,y,0) and then remove the wolf and bandit from the script since you’re not using those.

Can I ask a question, what does your Transform object contains of ?

–

Can you rephrase the question? I don't know what you mean.

–