Skip to content

Commit a653b06

Browse files
committed
add TerrainTreeReplacer
1 parent 9cecf7e commit a653b06

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
6+
public class TerrainTreeReplacer : EditorWindow
7+
{
8+
private const string RootObjectName = "TREES_CONVERTED";
9+
10+
[MenuItem("Window/Tools/Terrain Tree Replacer")]
11+
public static void ShowWindow()
12+
{
13+
EditorWindow.GetWindow(typeof(TerrainTreeReplacer));
14+
}
15+
16+
private Terrain terrain;
17+
18+
private bool disableDrawTreesAndFoliage = false;
19+
private int treeDivisions = 0;
20+
21+
private bool DivideTreesIntoGroups { get { return treeDivisions > 0; } }
22+
23+
void OnGUI()
24+
{
25+
GUILayout.Label("Replace Terrain Trees with Objects", EditorStyles.boldLabel);
26+
27+
terrain = EditorGUILayout.ObjectField("Terrain:", terrain, typeof(Terrain), true) as Terrain;
28+
disableDrawTreesAndFoliage = EditorGUILayout.ToggleLeft("Disable Drawing Trees and Foliage", disableDrawTreesAndFoliage);
29+
30+
GUILayout.Label("Tree Division groups: " + treeDivisions);
31+
treeDivisions = (int)GUILayout.HorizontalSlider(treeDivisions, 0, 10);
32+
33+
if (GUILayout.Button("Replace Terrain trees to Objects!")) Replace();
34+
if (GUILayout.Button("Clear generated trees!")) Clear();
35+
}
36+
37+
public void Replace()
38+
{
39+
if (terrain == null)
40+
{
41+
Debug.LogError("Please Assign Terrain");
42+
return;
43+
}
44+
45+
Clear();
46+
47+
GameObject treeParent = new GameObject(RootObjectName);
48+
49+
List<List<Transform>> treegroups = new List<List<Transform>>();
50+
51+
if (DivideTreesIntoGroups)
52+
{
53+
for (int i = 0; i < treeDivisions; i++)
54+
{
55+
treegroups.Add(new List<Transform>());
56+
for (int j = 0; j < treeDivisions; j++)
57+
{
58+
GameObject treeGroup = new GameObject("TreeGroup_" + i + "_" + j);
59+
treeGroup.transform.parent = treeParent.transform;
60+
treegroups[i].Add(treeGroup.transform);
61+
}
62+
}
63+
}
64+
65+
TerrainData terrainData = terrain.terrainData;
66+
67+
float xDiv = terrainData.size.x / (float)treeDivisions;
68+
float zDiv = terrainData.size.z / (float)treeDivisions;
69+
70+
foreach (TreeInstance tree in terrainData.treeInstances)
71+
{
72+
GameObject treePrefab = terrainData.treePrototypes[tree.prototypeIndex].prefab;
73+
74+
Vector3 position = Vector3.Scale(tree.position, terrainData.size);
75+
int xGroup = (int)(position.x / xDiv);
76+
int zGroup = (int)(position.z / zDiv);
77+
78+
position += terrain.transform.position;
79+
80+
Vector2 lookRotationVector = new Vector2(Mathf.Cos(tree.rotation - Mathf.PI), Mathf.Sin(tree.rotation - Mathf.PI));
81+
Quaternion rotation = Quaternion.LookRotation(new Vector3(lookRotationVector.x, 0, lookRotationVector.y), Vector3.up);
82+
83+
Vector3 scale = new Vector3(tree.widthScale, tree.heightScale, tree.widthScale);
84+
85+
GameObject spawnedTree = Instantiate(treePrefab, position, rotation) as GameObject;
86+
spawnedTree.name = treePrefab.name;
87+
88+
spawnedTree.transform.localScale = scale;
89+
90+
if (DivideTreesIntoGroups) spawnedTree.transform.SetParent(treegroups[xGroup][zGroup]);
91+
else spawnedTree.transform.SetParent(treeParent.transform);
92+
}
93+
94+
if (disableDrawTreesAndFoliage) terrain.drawTreesAndFoliage = false;
95+
}
96+
97+
public void Clear()
98+
{
99+
DestroyImmediate(GameObject.Find(RootObjectName));
100+
}
101+
}

0 commit comments

Comments
 (0)