using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 多边形 顺时针 先添加中心点 (0,0), 在添加N个顶点就可以了
/// [转] http://www.xshadow.org/index.php/archives/73
///
public class UIPolygon : Graphic
{
public List m_VertexList = new List(); // 顶点坐标列表 数量必须大于2
private VertexHelper m_VertexHelper;
public void Refresh()
{
if (m_VertexHelper == null)
{
Debug.Log("m_VertexHelper == null");
return;
}
m_VertexHelper.Clear();
List targetVertexList = new List();
int triangleCount = m_VertexList.Count - 1;
for (int i = 0; i < triangleCount; i++)
{
// 组建三角形
for (int j = 0; j < 3; j++)
{
UIVertex vertex = new UIVertex();
if (j == 2)
vertex.position = m_VertexList[0];
else
{
int vertexIndex = i + j + 1;
if (vertexIndex > triangleCount)
vertex.position = m_VertexList[1];
else
vertex.position = m_VertexList[vertexIndex];
}
vertex.color = color;
if (j == 2)
//vertex.color = new Color(color.r, color.g, color.b, 0);
vertex.color = new Color(0,0,0, 0);
targetVertexList.Add(vertex);
}
}
m_VertexHelper.AddUIVertexTriangleStream(targetVertexList);
}
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
m_VertexHelper = vh;
if (m_VertexList == null)
return;
if (m_VertexList.Count < 3)
return;
Refresh();
}
}