Firstborn/Assets/Scripts/TreeReplacerS.cs
Schaken-Mods f35c8a6d6e 6/21/23 update
Upgraded the framework which includes the Distant cell rendering. I have been merging meshes to cut down the Draw calls, so far gaining on average 20FPS everywhere. New bugs are the magic fails (Again) and the environment presets fail to call when outside. Currently trying to build new lightmaps for the combined meshes.
2023-06-21 11:09:52 -05:00

59 lines
2.0 KiB
C#

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("Schaken-Mods/Tree Replacer Tool")]
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("Object_Cluster");
if (parent == null)
{
parent = new GameObject("Object_Cluster");
}
// 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("Object_Cluster"));
}
}