public static void MassPlaceTrees(TerrainData terrainData, int numberOfTrees, bool randomTreeColor, bool keepExistingTrees)
{
int length = terrainData.treePrototypes.Length;
if (length == 0)
{
Debug.Log("Can't place trees because no prototypes are defined");
}
else
{
Undo.RegisterCompleteObjectUndo(terrainData, "Mass Place Trees");
TreeInstance[] sourceArray = new TreeInstance[numberOfTrees];
int num2 = 0;
while (num2 < sourceArray.Length)
{
TreeInstance instance = new TreeInstance {
position = new Vector3(Random.value, 0f, Random.value)
};
if (terrainData.GetSteepness(instance.position.x, instance.position.z) < 30f)
{
instance.color = !randomTreeColor ? Color.white : GetTreeColor();
instance.lightmapColor = Color.white;
instance.prototypeIndex = Random.Range(0, length);
instance.heightScale = GetTreeHeight();
instance.widthScale = !lockWidthToHeight ? GetTreeWidth() : instance.heightScale;
instance.rotation = GetTreeRotation();
sourceArray[num2++] = instance;
}
}
if (keepExistingTrees)
{
TreeInstance[] treeInstances = terrainData.treeInstances;
TreeInstance[] destinationArray = new TreeInstance[treeInstances.Length + sourceArray.Length];
Array.Copy(treeInstances, 0, destinationArray, 0, treeInstances.Length);
Array.Copy(sourceArray, 0, destinationArray, treeInstances.Length, sourceArray.Length);
sourceArray = destinationArray;
}
terrainData.treeInstances = sourceArray;
terrainData.RecalculateTreePositions();
}
}