首页 > 其他分享 >29. unity代码种树之删除树

29. unity代码种树之删除树

时间:2022-10-20 00:55:39浏览次数:45  
标签:terrain treeInstances 29 terrainData unity 种树 new treeInstances2 treeInstance

从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

相关文章

  • winXP immunity debugger 运行mona插件报错,解决方法
    报错内容报错内容提示如下:AttributeError:'list'objecthasnoattribute'update'找到问题所在为:File"C:\DocumentsandSettings\lrt\桌面\ImmunityDebugger-mast......
  • ctfshow web129(目录穿越)
    if(isset($_GET['f'])){$f=$_GET['f'];if(stripos($f,'ctfshow')>0){echoreadfile($f);}}用php伪协议绕过,filter伪协议支持多种编码方式,无......
  • Unity IO 加载图片
    ///<summary>///以IO方式进行加载///</summary>privatevoidLoadByIo(stringurl,GameObjectImageOBJ){//创建文件读取流FileStream......
  • unity3D mirror网络游戏开发笔记
    最近想开发一款多人在线网络游戏,使用unity3d的mirror插件可以提高开发效率,并且该插件免费。但是由于使用插件开发效率太低,经过研究,ummorpg开发模板是居于该插件开发而来,使......
  • C#与unity学习
    C#与unity学习unity中产创建c#文件在unity中新建project,为方便管理可以在assets文件内创建scripts文件夹,将所有的c#文件集中在此文件夹中管理。在vsstudio中创建c#​ ......
  • ADS1299开发调试总结之寄存器使用说明简析
    一前记  在生物生理信号测量领域,ads12xx系列是一个无法绕过去的存在。笔者最近几个项目围绕着动物生理信号测量来做。所以用ads12xx比较多一些。中间遇到了一些问题,......
  • 【LeetCode】1295. 统计位数为偶数的数字(C++)
    1295.统计位数为偶数的数字(C++)​​1题目描述​​​​2示例描述​​​​2.1示例1​​​​2.2示例2​​​​3解题提示​​​​4源码详解(C++)​​1题目描述你一个整数......
  • 【LeetCode】1299. 将每个元素替换为右侧最大元素(C++)
    1299.将每个元素替换为右侧最大元素(C++)​​1题目描述​​​​2示例描述​​​​2.1示例1​​​​2.2示例2​​​​3解题提示​​​​4源码详解(C++)​​1题目描述给......
  • Codeforces Round #729 (Div. 2) C
    C.StrangeFunction考虑反想我们将x确定看看有多少个i对于f[i]=x我们显然i%lcm(1,2,3,...x-1)!=0这里就可以通过容斥直接求解i%lcm(1,2,3,...x-1)是含有1,2,3,...x-1......
  • 我的收藏周刊029
    文章分享Nature历年网络科学文集:动力学与传播网络科学是一系列概念的集合,而不是特定某种结束,上学的时候对于这一点理解有偏差,导致产生了一种抵触的执念。南开大学......