Runtime terrain is actually not so hard now that Unity have made the properties available to the public.
Check out http://unity3d.com/support/documentation/ScriptReference/TerrainData.html - this is the object you’ll be playing with to create your terrain.
Your Terrain object has a single terrainData member that you can access to make changes.
TerrainData.GetHeights and TerrainData.SetHeights are used to set the height for sections of your map. The heights are stored as a 2-dimensional array of floats.
float[,] HeightMap = MyTerrain.terrainData.GetHeights();
The terrain is broken into a grid (the height and width of the grid are attributes of the TerrainData) and there is a height recorded at each point. So HeightMap[0,0] will be the height of the terrain in the top-left corner. HeightMap[2,4] will be 3 points across and 5 points down.
To randomise your terrain, simply fill an array with random values. But if you do this, you may be best off by then smoothing your values by averaging them with each-other, otherwise you can end up with a very spiky terrain.
//This example shows how to flatten your entire terrain to a height of 20...
TerrainData TD = MyTerrain.terrainData;
float[,] HeightMap = new float[TD.heightmapWidth,TD.heightmapHeight];
for(int x=0;x<TD.heightmapWidth;x++)
{
for(int y=0;y<TD.heightmapHeight;y++)
{
HeightMap[x,y] = 20;
}
}
TD.SetHeights(0,0,HeightMap);
Now, you can also do the same sort of thing with the various TEXTURES for your game world, using the AlphaMaps properties of your TerrainData.
This follows a similar pattern, but it’s a THREE dimensional array - one dimension is your x-coordinate, one is your y-coordinate, the last is the zero-based index of the textures you’ve added to your terrain.
So let’s say you have two different textures on your terrain - sand (#1) and grass (#2).
After you’ve generated your height map, you can then go through and apply textures based on the height - making lower sections sand and upper sections grass.
TerrainTextures[x,y,0] = 0.75f;
TerrainTextures[x,y,1] = 0.25f;
This would make point x,y 3/4 sand, with 1/4 of the grass texture coming through. You MUST normalise these values (ie. make sure they add up to 1).
Then use SetAlphamaps(0,0,TerrainTextures); to set it.