Currently I’m generating “terrain” as a mesh, and I’d like to turn it into a terrain object. I’m using the Object2Terrain script found here, where you point to an object in the GUI, then use menu options to configure the conversion. I’d like to accomplish this at runtime but am confused where the mesh object being converted is declared or pointed at in the code.
When going the GUI rout, the selected object is used. I see the check for an object selected, but the object is not passed into the CreateTerrain Function?
if(GUILayout.Button("Create Terrain")){
if(Selection.activeGameObject == null){
EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Ok");
return;
}
else{
CreateTerrain();
}
And when the CreateTerrain() function is called I also fail to see where the object is again specified anywhere:
public void CreateTerrain(TerrainData terrain, ){
//fire up the progress bar
ShowProgressBar(1, 100);
TerrainData terrain = new TerrainData();
terrain.heightmapResolution = 512f;
GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain);
Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain");
MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>();
CleanUp cleanUp = null;
//Add a collider to our source object if it does not exist.
//Otherwise raycasting doesn't work.
if(!collider){
collider = Selection.activeGameObject.AddComponent<MeshCollider>();
cleanUp = () => DestroyImmediate(collider);
}
Bounds bounds = collider.bounds;
float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y);
terrain.size = collider.bounds.size + addTerrain;
bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z);
//height Calculations
terrain.SetHeights(0, 0, heights)
So the “terrain” variable being manipulated is my Terrain Object, but it was set = new TerrainData(); . So how can I manipulate this so that I can attach it to my mesh objects that I want to convert?
Additionally, I’m somewhat confused with the TerrainData object I’m supposed to build out, what do I populate it with and how do I connect the two?