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
unity中利用mesh动态创建墙体
最新推荐文章于 2024-08-03 17:11:49 发布
本文介绍了在Unity引擎中如何利用Mesh组件动态创建墙体的方法,包括理解Mesh的基本构造、编写脚本生成墙体的几何形状,并结合Transform进行位置、旋转和缩放操作,实现游戏场景中的动态墙体生成。
摘要由CSDN通过智能技术生成