How to convert all Terrain trees to GameObjects?

I am working on a script which is supposed to convert all of the terrain trees in the scene to gameobjects (I made a billboard script, and I need to have basic scripts on the trees). Now, the problem is that the trees are not in the correct position when instantiated – I removed the code of destroying the terrain trees, and the trees instantiated are far from where they are supposed to be. Second, the trees do not respond to height. Please help me fix this problem. Thanks!

Code:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
public class CustomTreeBrush : EditorWindow
{

	Terrain terrain;

	GameObject[] trees = new GameObject[4];


	[MenuItem ("Tools/Custom/Terrain")]
	static void Init()
	{
		CustomTreeBrush window = (CustomTreeBrush)GetWindow (typeof(CustomTreeBrush));
	}

	void OnGUI()
	{
		terrain = (Terrain)EditorGUILayout.ObjectField (terrain, typeof(Terrain), true);

		trees[0] = (GameObject)EditorGUILayout.ObjectField (trees[0], typeof(GameObject), true);
		trees[1] = (GameObject)EditorGUILayout.ObjectField (trees[1], typeof(GameObject), true);
		trees[2] = (GameObject)EditorGUILayout.ObjectField (trees[2], typeof(GameObject), true);
		trees[3] = (GameObject)EditorGUILayout.ObjectField (trees[3], typeof(GameObject), true);

		if(GUILayout.Button("Convert to objects"))
		{
			Convert();
		}
	//	if(GUILayout.Button("Debug"))
	//	{
	//	}
	}

	public void Convert()
	{
		TerrainData data = terrain.terrainData;
		float width = (float)data.heightmapWidth;
		float height = (float)data.heightmapHeight;
		foreach(TreeInstance tree in data.treeInstances)
		{
			Vector3 position = new Vector3(tree.position.x * width, tree.position.y, tree.position.z * height);
			Instantiate(trees[tree.prototypeIndex], position, Quaternion.identity);
		}
	}

}

I have updated this script. It now takes tree prefabs from Terrain. It also sets position, rotation and scale of generated tree game objects correctly (according to terrain data):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
// Replaces Unity terrain trees with prefab GameObject.
// http://answers.unity3d.com/questions/723266/converting-all-terrain-trees-to-gameobjects.html
[ExecuteInEditMode]
public class TreeReplacerS : EditorWindow {
    [Header("References")]
    public Terrain _terrain;
    //============================================
    [MenuItem("Window/My/TreeReplacer")]
    static void Init()
    {
        TreeReplacerS window = (TreeReplacerS)GetWindow(typeof(TreeReplacerS));
    }
    void OnGUI()
    {
        _terrain = (Terrain)EditorGUILayout.ObjectField(_terrain, typeof(Terrain), true);
        if (GUILayout.Button("Convert to objects"))
        {
            Convert();
        }
        if (GUILayout.Button("Clear generated trees"))
        {
            Clear();
        }
    }
    //============================================
    public void Convert()
    {
        TerrainData data = _terrain.terrainData;
        float width = data.size.x;
        float height = data.size.z;
        float y = data.size.y;
        // Create parent
        GameObject parent = GameObject.Find("TREES_GENERATED");
        if (parent == null)
        {
            parent = new GameObject("TREES_GENERATED");
        }
        // Create trees
        foreach (TreeInstance tree in data.treeInstances)
        {
            if (tree.prototypeIndex>=data.treePrototypes.Length)
                continue;
            var _tree = data.treePrototypes[tree.prototypeIndex].prefab;
            Vector3 position = new Vector3(
                tree.position.x * width,
                tree.position.y*y,
                tree.position.z * height) + _terrain.transform.position;
            Vector3 scale = new Vector3(tree.widthScale,tree.heightScale,tree.widthScale);
            GameObject go = Instantiate(_tree, position, Quaternion.Euler(0f,Mathf.Rad2Deg*tree.rotation,0f), parent.transform) as GameObject;
            go.transform.localScale  = scale;
        }
    }
    public void Clear()
    {
        DestroyImmediate(GameObject.Find("TREES_GENERATED"));
    }
}

@anunnaki2016 to use this script you need to save it at Assets/Editor/TreeReplacerS.cs.
Now you are able to open this tool with Window/My/TreeReplacer menu item.

You are using the width and height of the heightmap, which is basically the resolution of the heightmap. Try using

public void Convert()
{
  TerrainData data = terrain.terrainData;
  float width = data.size.x;
  float height = data.size.z;
  float y = data.size.y;
  foreach(TreeInstance tree in data.treeInstances)
  {
    Vector3 position = new Vector3(tree.position.x * width, tree.position.y*y, tree.position.z * height);
    Instantiate(trees[tree.prototypeIndex], position, Quaternion.identity);
  }
}

instead. TerrainData.size is the actual size of the terrain in world units.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

// Replaces Unity terrain trees with prefab GameObject.
// http://answers.unity3d.com/questions/723266/converting-all-terrain-trees-to-gameobjects.html
[ExecuteInEditMode]
public class TreeReplacerS : EditorWindow
{
	
	[Header("Settings")]
	public GameObject _tree;
	
	[Header("References")]
	public Terrain _terrain;
	
	//============================================
	
	[MenuItem("Window/My/TreeReplacer")]
	static void Init()
	{
		TreeReplacerS window = (TreeReplacerS)GetWindow(typeof(TreeReplacerS));
	}
	
	void OnGUI()
	{
		_terrain = (Terrain)EditorGUILayout.ObjectField(_terrain, typeof(Terrain), true);
	
		_tree = (GameObject)EditorGUILayout.ObjectField(_tree, typeof(GameObject), true);
	
		if (GUILayout.Button("Convert to objects"))
		{
			Convert();
		}
	
		if (GUILayout.Button("Clear generated trees"))
		{
			Clear();
		}
	}
	
	//============================================
	
	public void Convert()
	{
		TerrainData data = _terrain.terrainData;
		float width = data.size.x;
		float height = data.size.z;
		float y = data.size.y;
	
		// Create parent
		GameObject parent = GameObject.Find("TREES_GENERATED");
	
		if (parent == null)
		{
			parent = new GameObject("TREES_GENERATED");
		}
	
		// Create trees
		foreach (TreeInstance tree in data.treeInstances)
		{
			//Vector3 position = new Vector3(tree.position.x * width, tree.position.y * y, tree.position.z * height);
			Vector3 position = new Vector3(tree.position.x * data.detailWidth - (data.size.x / 2), tree.position.y * y - (data.size.y / 2), tree.position.z * data.detailHeight - (data.size.z / 2));
	
			//Instantiate(_trees[tree.prototypeIndex], position, Quaternion.identity, parent.transform);
			Instantiate(_tree, position, Quaternion.identity, parent.transform);
		}
	}
	
	public void Clear()
	{
		DestroyImmediate(GameObject.Find("TREES_GENERATED"));
	}

}

Thank you for that script @joeysipos its realy usefull for me.
Another question to all of you.
Is there a way of converting the terrain ground to gameobject?
I am a C# & Unity beginner.
For any suggestion I would be very happy.

Hello, thank you for this tool.

I had the problem with the position os the meshs exported, so i changed this line.

Vector3 position = Vector3.Scale(tree.position, data.size) + _terrain.transform.position;

Its working for me now. bye.