从treeinstance[] treeInstances 里删除树 然后
设置terraindata.treeinstances=treeInstances 更新terraindata
再刷新地图数据 terrain.Flush();
即可
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
TreeInstance[] treeInstances = new TreeInstance[1];//terraindata里树的实例数组数据。
List<TreeInstance> treeInstances2 = new List<TreeInstance>();
int treecount = 10;
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 }; //设置树的预制数组
//树的数量,图中数量大于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;
treeInstances2.Add(treeInstance);
}
treeInstances = treeInstances2.ToArray();
terrainData.SetTreeInstances(treeInstances, true);
terrain.Flush();
}
// Update is called once per frame
void Update()
{
}
public void removeatree()
{
treeInstances2.RemoveAt(0);
treeInstances = treeInstances2.ToArray();
terrain.terrainData.treeInstances = treeInstances;
terrain.Flush();
}
}
标签:terrain,treeInstances,29,terrainData,unity,种树,new,treeInstances2,treeInstance From: https://www.cnblogs.com/dandansang/p/16808338.html