Object2Terrain

Author: Eric Haines (Eric5h5); C# conversion by Jessy

Description
Converts an object mesh to a heightmap. This way you can create terrain meshes in a standard 3D app such as Blender or Maya and convert it to a Unity terrain. (See also TerrainObjExporter, which saves a Unity terrain as an .obj file.)

Usage
You must place the script in a folder named Editor in your project's Assets folder for it to work properly.

Click on an object in the scene view or hierarchy, then select Object to Terrain from the Terrain menu. The object is then converted to the heightmap in the active terrain. The object you're clicking on must contain the mesh itself--for example, selecting a root transform object, where the actual mesh is contained in the child object, won't work. If in doubt, simply open the mesh asset in the project view, then drag the bare mesh into the scene or hierarchy:



This function uses the axis-aligned bounding box of the mesh, so object rotations on the x and z axis other than 0, or rotations on the y axis other than multiples of 90 degrees, may give somewhat odd results.

You will probably need to adjust the height of the terrain to match the object mesh (using Set Resolution… in the Terrain menu).

Additional Usage Notes for C# version
The main difference with this version of the script involves the Size Adjustment parameter. After you choose the Object to Terrain menu item, a utility window will pop up with the field for adjustment of this parameter automatically selected. Try leaving it at zero at first – just hit return/enter to close the window and create the terrain. If necessary, try again with positive vales to grow the terrain horizontally, or negative values to shrink it (around +/- .01 is probably a good starting point). If you click outside the window, it will close, and the operation will be canceled.