所有的画图代码直接拖到Cube...即可
将物体的Collider修改成 Mesh Collider
球
![](https://i-blog.csdnimg.cn/blog_migrate/4d302f3ce52d0c7edfc36e8616c4bd92.png)
public int num = 8;
public float r = 5;
void Start()
{
VertexHelper vh=new VertexHelper();
float ang = 2 * Mathf.PI / num;
for (int i = 0; i < num/2+1; i++)
{
float y=Mathf.Cos(ang*i)*r;
float xr=Mathf.Sin(ang*i)*r;
float uvy = (float)i / (num / 2);
for (int j = 0; j < num; j++)
{
float x = Mathf.Sin(j * ang) * xr;
float z = Mathf.Cos(j * ang) * xr;
float uvx = (float)j / num;
vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2(uvx, uvy));
if(j==num-1)
{
float x0 = Mathf.Sin(0) * xr;
float z0 = Mathf.Cos(0) * xr;
vh.AddVert(new Vector3(x0, y, z0), Color.white, new Vector2(1, uvy));
}
if(i<num/2)
{
vh.AddTriangle(i * (num + 1) + j, (i + 1) * (num + 1) + j, i * (num + 1) + j + 1);
vh.AddTriangle(i * (num + 1) + j + 1, (i + 1) * (num + 1) + j , (i + 1) * (num + 1) + j+1);
}
}
}
Mesh mesh=new Mesh();
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
}
圆柱
![](https://i-blog.csdnimg.cn/blog_migrate/7b8d7167837c3f91ea1a3ae72dd66ca7.png)
float num = 20;
float angle;
float r = 5;
void Start()
{
angle = 2 * Mathf.PI / num;
Mesh mesh = new Mesh();
VertexHelper vh = new VertexHelper();
//0
vh.AddVert(Vector3.zero, Color.white,new Vector2(0.5f, 0.5f));
//1
vh.AddVert(new Vector3(0,1,0), Color.white,new Vector2(0.5f, 0.5f));
for (int i = 0; i < num; i++)
{
float x = Mathf.Sin(angle * i) * r;//x轴
float z=Mathf.Cos(angle * i) * r;//z轴
vh.AddVert(new Vector3(x, 0, z), Color.white, new Vector2(0.5f, 0.5f));
vh.AddVert(new Vector3(x, 1, z), Color.white, new Vector2(0.5f, 0.5f));
if(i==num-1)
{
//上面的面
vh.AddTriangle(3, 1, (i+1) * 2 + 1);
//下面的面
vh.AddTriangle((i + 1) * 2, 0,2 );
//边缘
vh.AddTriangle(3, (i + 1) * 2 + 1, (i + 1) * 2);
vh.AddTriangle(3, (i + 1) * 2, 2);
}
else
{
//上面的面
vh.AddTriangle(1, (i+1) * 2 + 1, (i + 2) * 2 + 1);
//下面的面
vh.AddTriangle((i + 2) * 2, (i+1) * 2 ,0 );
//边缘
vh.AddTriangle((i + 1) * 2 + 1, (i + 1) * 2, (i + 2) * 2 + 1);
vh.AddTriangle((i + 2) * 2 + 1, (i + 1) * 2, (i + 2) * 2);
}
}
vh.FillMesh(mesh);
GetComponent<MeshCollider>().sharedMesh = mesh;
GetComponent<MeshFilter>().mesh = mesh;
}
圆锥
![](https://i-blog.csdnimg.cn/blog_migrate/5e020f9f5fef8d4def4514aba465bea3.png)
float num=10;
float angle;
float r = 5;
void Start()
{
angle=2*Mathf.PI/num;
Mesh mesh = new Mesh();
VertexHelper vh=new VertexHelper();
vh.AddVert(Vector3.zero, Color.blue,new Vector2(0.5f, 0.5f));
vh.AddVert(new Vector3(0,1,0), Color.blue,new Vector2(0.5f, 0.5f));
for(int i=0; i<num; i++)
{
float x = Mathf.Sin(angle * i) * r;
float z = Mathf.Cos(angle * i) * r;
float uvx = (x + r) / (2 * r);
float uvz = (z + r) / (2 * r);
vh.AddVert(new Vector3(x, 0, z), Color.blue, new Vector2(uvx, uvz));
if(i==num-1)
{
vh.AddTriangle(i+2, 0, 2);//底
vh.AddTriangle(1, i + 2, 2);
}
else
{
vh.AddTriangle(0, i + 3, i + 2);//底
vh.AddTriangle(1, i + 2, i + 3);
}
}
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh=mesh;
GetComponent<MeshCollider>().sharedMesh=mesh;
}