using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class Radar : MonoBehaviour
{
[Tooltip("属性边长")]
public float radius = 45;
[Range(0, 1), Tooltip("当前属性百分比")]
public List<float> m_attributes;
private float m_cell_angle;
private List<Vector3> m_vertices = new List<Vector3>();
private int[] m_triangles;
private MeshRenderer m_render;
private MeshFilter m_filter;
private Mesh m_mesh;
private void Awake()
{
m_render = GetComponent<MeshRenderer>();
m_filter = GetComponent<MeshFilter>();
m_mesh = new Mesh();
m_filter.mesh = m_mesh;
if (m_render.material == null)
{
Debug.LogWarning("请添加Material");
}
}
void Start()
{
if (m_attributes.Count != 0)
{
m_cell_angle = 360 / m_attributes.Count;
InitMeshData();
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Refresh();
}
}
void InitMeshData()
{
#region 设置顶点
m_vertices.Clear();
m_vertices.Add(new Vector3(0, 0, 1));
m_vertices.Add(new Vector3(radius, 0, 1));
for (int i = 0; i < m_attributes.Count; i++)
{
float angle = Mathf.Deg2Rad * m_cell_angle * (i + 1);
Vector3 point = new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 1);
m_vertices.Add(point);
}
#endregion
#region 设置三角形
m_triangles = new int[m_attributes.Count * 3];
int index = 0;
int value = 0;
for (int i = 0; i < m_triangles.Length; i++)
{
if (i % 3 == 0)
{
m_triangles[i] = 0;
value = index;
index++;
}
else
{
value++;
if (value == m_attributes.Count + 1)
value = 1;
m_triangles[i] = value;
}
}
#endregion
}
void Refresh()
{
Vector3[] vertices = m_vertices.ToArray();
for (int i = 2; i < m_vertices.Count; i++)
{
vertices[i] = m_vertices[i] * m_attributes[i - 2];
}
m_mesh.vertices = vertices;
m_mesh.triangles = m_triangles;
m_mesh.RecalculateNormals();
}
}
View Code
转载:https://blog.csdn.net/zjw1349547081/article/details/53667690