暂时未实现删除

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{

public Terrain terrain;

public GameObject treePrefab;
// Start is called before the first frame update
void Start()
{

TerrainData terrainData = terrain.terrainData;
terrainData.heightmapResolution = 17; //将地图变小。不然树太小了看不见。。。

TreePrototype treePrototype = new TreePrototype();
treePrototype.prefab = treePrefab;
treePrototype.bendFactor = 1;
terrainData.treePrototypes = new TreePrototype[1] { treePrototype }; //树的预制数组

 


int treecount = 10; //树的数量,图中数量大于10是因为运行了多次,树未删除。

for(int i=0;i<treecount;i++)
{

TreeInstance treeInstance = new TreeInstance();

treeInstance.widthScale = 1f;
treeInstance.heightScale = 1f;
treeInstance.color = Color.white;
treeInstance.lightmapColor = Color.white;
treeInstance.position = new Vector3(Random.Range(0f, 1f), 0, Random.Range(0f, 1f)); //位置为0-1.0f
treeInstance.prototypeIndex =0;
terrain.AddTreeInstance(treeInstance);

}

 

}

// Update is called once per frame
void Update()
{

}

 

 


}
//
// 摘要:
// Contains information about a tree placed in the Terrain game object
//public struct TreeInstance
//{
// //
// // 摘要:
// // Position of the tree.
// public Vector3 position;
// //
// // 摘要:
// // Width scale of this instance (compared to the prototype's size).
// public float widthScale;
// //
// // 摘要:
// // Height scale of this instance (compared to the prototype's size).
// public float heightScale;
// //
// // 摘要:
// // Read-only. Rotation of the tree on X-Z plane (in radians).
// public float rotation;
// //
// // 摘要:
// // Color of this instance.
// public Color32 color;
// //
// // 摘要:
// // Lightmap color calculated for this instance.
// public Color32 lightmapColor;
// //
// // 摘要:
// // Index of this instance in the TerrainData.treePrototypes array.
// public int prototypeIndex;
//}