Created
May 31, 2018 00:51
-
-
Save cjacobwade/5b22832a3902e44410e9837350b6085a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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