- Home /
Clearing the TreeInstance array removes all trees perminantly
Hey guys. I wanted to use the terrain tree painting tools to create nice woods. However the terrain trees don't offer all I want so I have an initializer that runs through the TreeInstance array and creates full on GameObject trees. However, when I set the TreeInstance array to an empty array at run time- to get rid of the terrain trees, this removes the trees from the designer too.
I'll include the script that is attached to the terrain.
public class TerrainInitaliser : MonoBehaviour
{
public ArrayList treeArray;
// Use this for initialization
void Start ()
{
// Grab the tree array from the terrain
treeArray = new ArrayList(Terrain.activeTerrain.terrainData.treeInstances);
int originalCount = treeArray.Count -1;
Debug.Log("Tree instances: " + originalCount);
// Substitute all the trees for game objects
for(int i = 0; i <= originalCount; i++)
{
GameObject newTree = null;
switch(((TreeInstance)treeArray[0]).prototypeIndex)
{
case 0 :
{
newTree = GameObject.Instantiate(Resources.Load("PoC Prefabs/OakTree")) as GameObject;
}; break;
case 1 :
{
newTree = GameObject.Instantiate(Resources.Load("PoC Prefabs/PalmTree")) as GameObject;
}; break;
default :
{
return;
}
}
// Set the properties
newTree.transform.position = new Vector3(
Terrain.activeTerrain.terrainData.size.x * ((TreeInstance)treeArray[0]).position.x,
Terrain.activeTerrain.terrainData.size.y * ((TreeInstance)treeArray[0]).position.y,
Terrain.activeTerrain.terrainData.size.z * ((TreeInstance)treeArray[0]).position.z
);
newTree.transform.localScale = new Vector3(
((TreeInstance)treeArray[0]).widthScale,
((TreeInstance)treeArray[0]).heightScale,
((TreeInstance)treeArray[0]).widthScale
);
// Remove this tree
treeArray.RemoveAt(0);
}
// Remove the built-in trees from the terrain
TreeInstance[] tmpArray = new TreeInstance[0];
treeArray.CopyTo(tmpArray);
Terrain.activeTerrain.terrainData.treeInstances = tmpArray;
// Refresh the terrain
float[,] heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 0, 0);
Terrain.activeTerrain.terrainData.SetHeights(0, 0, heights);
Debug.Log("Done");
}
}
Answer by getyour411 · Feb 15, 2014 at 12:45 AM
That is correct.
Well why is this? I thought changes made during run-time only exist during run-time. Is there a better way of doing what I am currently doing?
TerrainData is different. If you hit play and then in another docked Scene view modify some terrain (hills, smooth, whatever) and then stop playing, it doesn't go back it's a one way train. There's no good solution I can think of short of writing the contents of the original tree/terraindata out to filesystem because keeping it in memory alone won't suffice, then you'd need to read it back in on Play. Difficult.
Damn. Fair enough. And I thought using the built-in tree system was going to make life easy! I guess I am going to have to write my own tree populator tool or something. I'm planning on making maps with quite a number of trees so planting them by hand would be pretty intensive.
I'm in the same boat, which is why I always looking at Tree questions to see if other solutions are offered, there may be an alternative I'm not aware of. Also, by Difficult I didn't mean Impossible - there exists tools in the Unity wiki like PlayerPrefs2 and others that help write array contents to disk, I just haven't tackled it yet.
Answer by Remingsworth · Dec 16, 2015 at 08:39 AM
The terrainData is being modified at runtime and does not realize it is supposed to change back to it's unaltered state. You have to tell the terrainData to reset at runtime exit. You can fix by adding a script to your terrain.
public class TerrainScript : MonoBehaviour {
private Terrain terrain;
public TreeInstance[] originalTrees;
void Start () {
terrain = GetComponent<Terrain> ();
//save original trees
originalTrees = terrain.terrainData.treeInstances;
}
void OnApplicationQuit() {
// restore original trees
terrain.terrainData.treeInstances = originalTrees;
}
}
Your answer
Follow this Question
Related Questions
Trees not lighting properly 0 Answers
Trees on slopes are floating 1 Answer
Tree painter problem 1 Answer
How do I get a speedtree color? 0 Answers