Generating and altering a mesh at runtime as a grid based terrain

Hey guys,
I’ve just gotten stuck into my first big project with Unity (loving it BTW) and I’ve hit a wall.
The game is going to be using a tile grid style map that is procedurally generated and will be able to have buildings placed upon it by the player.
I have been experimenting with using individual prefabs for each tile shape (flat, sloped)and then replacing the material to indicate the tile’s type (grass, sand, etc.), but it takes a long time to set up at the start (when instantiating all the tiles) and it seems a little wasteful to have thousands of cube floating about.
I did a little research into generating a custom mesh at run time and then messing with the individual vertices to change the land’s shape, but I’m really having trouble wrapping my head around it.
Ideally, I’d like to end up with a landscape that works similarly to that in ‘Populous’ (google it for screen shots if you don’t know it) where I can just create a 2d array of tiles and use that data to set the vertices and materials of the “tiles” on the giant landscape mesh.
Can anybody give me some pointers as to how I’d go about achieving this? Should I just go back to the thousands of prefabs method? Is there a better way to go about achieving this kind of interactive landscape???

No, don’t use zillions of separate gameObjects. See the procedural examples for examples of using the Mesh class.

All the ground is, is a boring old 2D array of floats: float[,] HT; The detail texture strengths are stored in a regular 3D array (2D for the ground, the 3rd dimension is the strength of each texture. D[6,3,1]=0.25 sets the second texture at ground grid (6,3) to be quarter strength. Make sure D[6,3,*] adds to 1.)

The trickiest part is remembering you are setting corners of squares, and not squares. And keeping straight that if your ground squares are 5x5 meters, than world 38,12 is just past terrain corner 7,2.

Look at TERRAINDATA for all the names. You can’t change the size of the ground array (Width/Height) that I know of, so make a Terrain of the correct size first. May as well manually load all the detail textures you want. SetHeights copies your array to the ground. SetAlphaMaps copies your detail strengths.

Not sure how to look up the “World width” of each terrain square (think it might be in AlphaMapWidth,) but you can compute it with size/heightMapWidth. I’d make/change some terrains and just print all the vars, just to be sure.

Lastly, to make changes “stick,” you have to use GetComponent<Terrain>().Flush(); (listed in Terrain, and not TerrainData.)

I’ve solved a similar problem, albeit not for terrain. I have thousands of meshes that need to be created/released based on player position. I use a combination of the object recycler pattern - i’ll call it a pattern because it’s basically become the accepted way to do reuse of large numbers of instantiated objects. In concert with the object recycler, I have a mesh loader which changes out the verts/tris/norms of the object’s mesh on the next available free object from the recycler . Coroutines keep this working without locking the interface, although you can expect stuttering based on the size and complexity of the meshes that you instantitate. To your point about wastefulness, yes it is wasteful in one sense to have all those cubes instantiated at startup, but it is much slower to instantiate them in coroutines at run time. I can’t vouch for any of the precooked tile solutions, as my solution needed to be a bit custom, but there might be something that fits your requirements.