I am using this piece of code to place my trees, it is constantly using 300+ ms:
IEnumerator SpawnTheTrees() {
if (TreesPlaced) {
return false;
}
TreesPlaced = true;
TreeInstance treeInstance = new TreeInstance ();
treeInstance.color = Color.white;
treeInstance.lightmapColor = Color.white;
for (int x = 0; x < Ah; x++) {
float normalPosX = (x * 1f) / (Ah * 1f);
for (int z = 0; z < Ah; z++) {
float normalPosZ = (z * 1f) / (Ah * 1f);
float height = Chunk.terrainData.GetInterpolatedHeight(normalPosX,normalPosZ);
float angle = Chunk.terrainData.GetSteepness(normalPosX,normalPosZ);
int random = Random.Range (0, 100000);
Biome Biome = Biomes.GetPoint (x,z);
if (random < Biome.TreeChance && height > WaterLevel+5 && angle < 25) {
treeInstance.prototypeIndex = Random.Range (0,treePrototypes.Length);
treeInstance.position = new Vector3(normalPosX,height,normalPosZ);
Trees++;
Chunk.AddTreeInstance(treeInstance);
}
}
if (x % 10 == 0) {
progress = (x * 1f) / (Ah * 1f);
yield return null;
}
}
Chunk.GetComponent().enabled = false;
Chunk.GetComponent().enabled = true;
progress = 1.0f;
}
↧