Skip to content

Instantly share code, notes, and snippets.

@cjacobwade
Created May 31, 2018 00:51
Show Gist options
  • Save cjacobwade/5b22832a3902e44410e9837350b6085a to your computer and use it in GitHub Desktop.
Save cjacobwade/5b22832a3902e44410e9837350b6085a to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public static class ExportTerrainTrees
{
[MenuItem("Utilities/Export Terrain Trees")]
static void ExportTrees()
{
Terrain terrain = Selection.activeGameObject.GetComponent<Terrain>();
if(terrain)
{
GameObject[] prefabInstances = new GameObject[terrain.terrainData.treePrototypes.Length];
for (int i = 0; i < terrain.terrainData.treePrototypes.Length; i++)
prefabInstances[i] = terrain.terrainData.treePrototypes[i].prefab;
foreach (TreeInstance tree in terrain.terrainData.treeInstances)
{
GameObject spawnedTree = GameObject.Instantiate(prefabInstances[tree.prototypeIndex],
Vector3.Scale(tree.position, terrain.terrainData.size) + terrain.GetPosition(),
Quaternion.Euler(0f, tree.rotation, 0f));
Vector3 pos = spawnedTree.transform.position;
Quaternion rot = spawnedTree.transform.rotation;
spawnedTree = PrefabUtility.ConnectGameObjectToPrefab(spawnedTree, prefabInstances[tree.prototypeIndex]);
spawnedTree.transform.position = pos;
spawnedTree.transform.rotation = rot;
}
}
}
[MenuItem("Sausage Soccer/Export Terrain Trees", true)]
static bool ExportTreesValidate()
{
if(Selection.activeGameObject)
return Selection.activeGameObject.GetComponent<Terrain>();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment