1,为圆设置20个顶点,利用顶点助手先给圆添加一个圆点,然后添加uv坐标
2,给VertexHelper的AddVert方法进行赋值,然后在给AddTriangle方法进行赋值
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreateCircle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int n = 20;
int r= 2;
float ang = 2 * Mathf.PI / n;
VertexHelper vh = new VertexHelper();
vh.AddVert(Vector3.zero, Color.white, new Vector2(0.5f, 0.5f));
Mesh mesh = new Mesh();
for (int i = 0; i < n; i++)
{
float x = Mathf.Sin(ang * i) * r;
float y = Mathf.Cos(ang * i) * r;
float uvx = (x + r) / (2 * r);
float uvy = (y + r) / (2 * r);
vh.AddVert(new Vector3(x, y, 0), Color.white, new Vector2(uvx, uvy));
if(i==n-1)
{
vh.AddTriangle(0, i+1, 1);
}
else
{
vh.AddTriangle(0, i + 1, i + 2);
}
}
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
}
// Update is called once per frame
void Update()
{
}
}