UnityEditor.TreePainter.MassPlaceTrees C# (CSharp) Method

MassPlaceTrees() public static method

public static MassPlaceTrees ( TerrainData terrainData, int numberOfTrees, bool randomTreeColor, bool keepExistingTrees ) : void
terrainData UnityEngine.TerrainData
numberOfTrees int
randomTreeColor bool
keepExistingTrees bool
return void
        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();
            }
        }

Usage Example

 void OnWizardCreate()
 {
     if (numberOfTrees > kMaxNumberOfTrees)
     {
         isValid     = false;
         errorString = String.Format("Mass placing more than {0} trees is not supported", kMaxNumberOfTrees);
         Debug.LogError(errorString);
         return;
     }
     TreePainter.MassPlaceTrees(m_Terrain.terrainData, numberOfTrees, true, keepExistingTrees);
     m_Terrain.Flush();
 }
All Usage Examples Of UnityEditor.TreePainter::MassPlaceTrees