Creating TerrainData.Asset programatically

How do I create Terrain from script, I know about Terrain.CreateTerrainGameObject(), but this function accept TerrainData as an argument, and then by this forum topic the TerrainData instance must already be in Project view.

My question is how do I create that TerrainData instance from my script (without using Unity default Terrain tools). I’am planning to create custom heightmap importer for my terrain.

Try this:

	TerrainData terrainData = new TerrainData ();
	const int size = 513;
	terrainData.heightmapResolution = size;
	terrainData.size = new Vector3 (2000, 600, 2000);
	terrainData.heightmapResolution = 512;
	terrainData.baseMapResolution = 1024;
	terrainData.SetDetailResolution(1024, terrainData.detailResolutionPerPatch);

	AssetDatabase.CreateAsset(terrainData, "Assets/New Terrain.asset");