using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(CanvasRenderer))]
public class Ldt : MaskableGraphic
{
//定义个数
int n = 5;
//定义半径
int r = 500;
//弧度
float ang = 0;
public float[] indexs;
public Button btn;
// Start is called before the first frame update
void Start()
{
ang = (2 * Mathf.PI) / n;
indexs = new float[n];
for (int i = 0; i < n; i++)
{
indexs[i] = 0.2f;
}
//按钮点击更新雷达图数据
btn.onClick.AddListener(() =>
{
for (int i = 0; i < n; i++)
{
indexs[i] = Random.Range(0.0f, 1.0f);
}
SetAllDirty();
});
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
vh.AddVert(Vector3.zero, Color.yellow, Vector2.zero);
for (int i = 0; i < n; i++)
{
float x = Mathf.Sin(i * ang) * r * Mathf.Clamp(indexs[i],0.2f,0.9f);
float y = Mathf.Cos(i * ang) * r * Mathf.Clamp(indexs[i], 0.2f, 0.9f);
vh.AddVert(new Vector3(x, y, 0), Color.yellow, Vector2.zero);
if (i == 0)
{
vh.AddTriangle(0, 1, n);
}
else
{
vh.AddTriangle(0, i + 1, i);
}
}
}
}
Unity简易版雷达图
于 2024-08-01 20:02:34 首次发布