- I would like to add a bit of seasonal flare to my trees added via the Terrain paint widget. The trees are not placed as GameObjects in the hierarchy.
There is not a function to swap between tree prefabs.
Unity Terrain exposes the Tree instances using the array Terrain.treeInstances. Every Tree instance included in this array has a property that stores the tree Prefab configured in the Terrain settings (prototypeIndex).
The tree Prefab is updated automatically by changing this value.
void ChangeSeasons ()
// The currentTreeList array must be at least terrain.treeInstances.Length in size
System.Array.Copy (terrain.treeInstances, currentTreeList, terrain.treeInstances.Length);
if (terrain.treeInstances.Length == currentTreeList.Length)
for (int tcnt=0; tcnt < currentTreeList.Length; tcnt++)
if (currentTreeList [tcnt].prototypeIndex == springTreeIndex)
currentTreeList [tcnt].prototypeIndex = fallTreeIndex;
if (currentTreeList [tcnt].prototypeIndex == fallTreeIndex)
currentTreeList [tcnt].prototypeIndex = springTreeIndex;
terrain.treeInstances = currentTreeList;
season = !season;