```csharp
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class SetTerrain : MonoBehaviour
{
Terrain terrain;
public float p = 0.1f;
public GameObject[] tree;
void Start()
{
terrain = GetComponent<Terrain>();
int wh = terrain.terrainData.heightmapResolution;
float[,] heights = new float[wh, wh];
for (int x = 0; x < wh; x++)
{
for (int z = 0; z < wh; z++)
{
float y = Mathf.PerlinNoise(x * p, z * p);
heights[x, z] = y;
}
}
terrain.terrainData.SetHeights(0,0,heights);
float[,,] element = terrain.terrainData.GetAlphamaps(0, 0, wh - 1, wh - 1);
for (int x = 0; x < wh-1; x++)
{
for (int z = 0; z < wh-1; z++)
{
float y = Mathf.PerlinNoise(x * p, z * p);
element[x, z, 0] = 1 - y;
element[x, z, 1] = y;
}
}
terrain.terrainData.SetAlphamaps(0,0,element);
TreePrototype[] trees = new TreePrototype[tree.Length];
for (int i = 0; i < tree.Length; i++)
{
trees[i] = new TreePrototype();
trees[i].prefab = tree[i];
}
terrain.terrainData.treePrototypes = trees;
for (int x = 0; x < wh-1; x++)
{
for (int z = 0; z < wh-1; z++)
{
if (Random.Range(0,360)==0)
{
TreeInstance instance = new TreeInstance();
instance.prototypeIndex = Random.Range(0, tree.Length);
instance.color = Color.white;
instance.lightmapColor = Color.white;
instance.widthScale = 1;
instance.heightScale = 1;
instance.position=new Vector3(x/(float)(wh-1),0,z/(float)(wh-1));
instance.rotation = Random.Range(0, 360);
terrain.AddTreeInstance(instance);
}
else if (Random.Range(0,100)==0)
{
TreeInstance instance = new TreeInstance();
instance.prototypeIndex = 0;
instance.color = Color.white;
instance.lightmapColor = Color.white;
instance.widthScale = 1;
instance.heightScale = 1;
instance.position=new Vector3(x/(float)(wh-1),0,z/(float)(wh-1));
instance.rotation = Random.Range(0,360);
terrain.AddTreeInstance(instance);
}
}
}
}