unity中利用mesh动态创建墙体

本文介绍了在Unity引擎中如何利用Mesh组件动态创建墙体的方法,包括理解Mesh的基本构造、编写脚本生成墙体的几何形状,并结合Transform进行位置、旋转和缩放操作,实现游戏场景中的动态墙体生成。
摘要由CSDN通过智能技术生成

private float _wallHeight=3.5f;
	private float _wallLength=3.0f;
	private float _wallWidth=0.24f;//默认的墙的厚度

	/***
	 * 
	 * params:vector2 表示起点平面坐标,wallLength表示墙的长度
	 * */
	Mesh createObject(Vector2 v1,Vector2 v2,float wallWidth,float wallHeight){
		Mesh mesh=new Mesh();

		Vector3[] points_bottom=getRectPositions(v1,v2,wallWidth,0);

		for(int m=0;m<points_bottom.Length;m++){
			GameObject obj=	GameObject.CreatePrimitive(PrimitiveType.Sphere) as GameObject;
			obj.transform.position=points_bottom[m];
			obj.transform.localScale=new Vector3(0.1f,0.1f,0.1f);
		}


		Vector3[] points_top=getRectPositions(v1,v2,wallWidth,wallHeight);

		for(int p=0;p<points_top.Length;p++){
			GameObject obj=	GameObject.CreatePrimitive(PrimitiveType.Cube) as GameObject;
			obj.transform.position=points_top[p];
			obj.transform.localScale=new Vector3(0.1f,0.1f,0.1f);
		}



		Vector3[] points_six=getRectPoints(points_top,points_bottom);

		Vector3[] points=new Vector3[points_six.Length];

	 
		points_six.CopyTo(points,0);

		int[] triangles   = new int[points.Length];
		int j=0;
		while(j<points.Length){
			triangles[j]=j;
			j++;
		}
		Vector2[] uvs=new Vector2[points.Length];
		int i=0;
		while(i<points.Length){
			uvs[i]=new Vector2(points[i].x,points[i].z);
			i++;
		}
		mesh.vertices=points;
		mesh.triangles=triangles;
		mesh.uv=uvs;
		return mesh;
	}
	 
	void OnGU
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值