Skip to content

Instantly share code, notes, and snippets.

@longde123
Forked from st4rdog/TreeReplacerS.cs
Created October 19, 2021 06:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longde123/1213796c3ce89436bed667aa1ff804b0 to your computer and use it in GitHub Desktop.
Save longde123/1213796c3ce89436bed667aa1ff804b0 to your computer and use it in GitHub Desktop.
Replaces trees on a terrain with prefab.
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("Tools/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);
GUILayout.Label("Create");
GUILayout.BeginHorizontal();
if (GUILayout.Button("Convert (Keep Previous)", GUILayout.Height(40f)))
{
Convert();
}
if (GUILayout.Button("Convert (Clear Previous)", GUILayout.Height(40f)))
{
Clear();
Convert();
}
GUILayout.EndHorizontal();
GUILayout.Label("Destroy");
GUILayout.BeginHorizontal();
Color oldColor = GUI.backgroundColor;
GUI.backgroundColor = Color.red;
if (GUILayout.Button("Clear generated trees", GUILayout.Height(40f)))
{
Clear();
}
if (GUILayout.Button("ClearTerrainTreeInstances", GUILayout.Height(40f)))
{
ClearTerrainTreeInstances();
}
GUI.backgroundColor = oldColor;
GUILayout.EndHorizontal();
}
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
for (int i = 0; i < data.treeInstances.Length; i++)
{
TreeInstance tree = data.treeInstances[i];
//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 as Prefab if is one, if not, instantiate as normal
GameObject go = PrefabUtility.InstantiatePrefab(_tree) as GameObject;
if (go != null)
{
go.name += " (" + i.ToString() + ")";
go.transform.position = position;
go.transform.parent = parent.transform;
}
else
{
Instantiate(_tree, position, Quaternion.identity, parent.transform);
}
}
}
public void Clear()
{
DestroyImmediate(GameObject.Find("TREES_GENERATED"));
}
public void ClearTerrainTreeInstances()
{
_terrain.terrainData.treeInstances = new List<TreeInstance>().ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment