Terrain Data Save (height field only)

Is there a good way to save only the height field values of a Unity Terrain? My goal is to save the values into an XML file and read them back in to create the same terrain.

Any suggestions?

Terrain.terrainData has a GetHeights() method that returns a float array. With that, you can export it out to absolutely anything you want.

That would be kind of slow and very inefficient, and pointless since you don’t need the heightmap numbers to be human-readable. It would be better to save the binary values…GetHeights() returns a 2D float array, which is a bit unfortunate since the actual values as used by the terrain engine are 0-65535, so really it should return a 2D ushort array. However it’s simple to convert the float values to ushorts, and a ushort is 2 bytes. Therefore you can convert each ushort to 2 bytes in a byte[ ] array and write that out with System.IO.File.WriteAllBytes, and use ReadAllBytes to read the file and reverse the process to create a 2D float array that you’d use with SetHeights. This would make the file far smaller and quite a bit quicker than parsing zillions of XML entries.

–Eric

Thanks to you both. I will look into both of these right away.