Converting a Plane to a grid

Hi guys,

I've got a large Plane like object which I've imported from Blender which is acting as my worlds 'floor'.

I'd like to programmatically split this into a grid - the grid doesn't have to show to the user but internally I need to know each grid square location and size.

My inital thoughts are to take the Mesh.bounds (or the Renderer.bounds) and divide the x and the z to get an even number of grid squares for the plane.

Is this the best approach?

I understand that doing it this way will most likely leave the last grid square on each row as an odd size so I would need to figure out how to fix that, but otherwise this is the only way I can think of to do this.

Any help much appreciated!

The "best approach" is decided by considering the use case. This is a subjective definition, perhaps too much so.

There are several ways to do grids as indicated in the questions tagged grid. You might start there.

Your approach seems fine. There are really two ways to approach dividing a space into a grid:

  1. Divide into n spaces.
  2. Divide into spaces of size n.

Since it's your model and you are looking for an even number of squares, could you not simply ensure that your model evenly divides to avoid odd grid squares on the outside edges?

Creating a class for grid tiles and storing some sort of array of them would be the typical approach to the internal description you've provided. Regardless of which of the above methods you've chosen, the size of your tiles will be known in advance so you shouldn't need to store them with each tile unless you are allowing for arbitrarily sized/shaped tiles which is a different problem entirely.

To get the position of the tiles would involve somehow dividing the space that you are dividing by either the size to get the number of tiles or by the number of tiles to get the size. You could get the size of the space from the bounds or from some raycasting or some other method to your liking like snapping some child empty gameObjects to your corner vertices. The "best" is determined by your use case, but the solution I'd recommend is the one that you have described if your space is axis-aligned. Storing the position might be needless as well as you can easily figure out where the tile is by its position in the array, the stored area size and the stored tile dimensions that are the same for all tiles and if you store your current position, it shouldn't be too hard to figure it out from there. If you are highlighting your tiles with a projector or something, then you would just increment the position with the tile size * the number of tiles moved, etc.